Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML: Use Download Attribute to Force File Download Instead of Opening

- 29.05.26 - ErcanOPAK

📥 Click to Download, Not to Preview

PDF opens in browser. Image displays. User right-clicks, “Save as”. download attribute forces download with custom filename.

📝 Basic Usage

<!-- Download with original filename -->
<a href="/files/report.pdf" download>Download PDF</a>

<!-- Download with custom filename -->
<a href="/files/report.pdf" download="Q4_Report_2024.pdf">
  Download PDF
</a>

<!-- Image download -->
<a href="image.jpg" download="vacation-photo.jpg">
  <img src="thumbnail.jpg" alt="Vacation">
</a>

🎯 Dynamic Content Download

<!-- Download from blob (generated content) -->
<a id="downloadLink">Download CSV</a>

<script>
const data = [{name: 'John'}, {name: 'Jane'}];
const csv = convertToCSV(data);
const blob = new Blob([csv], {type: 'text/csv'});
const url = URL.createObjectURL(blob);

const link = document.getElementById('downloadLink');
link.href = url;
link.download = 'users.csv';  // Forces download
link.click();

URL.revokeObjectURL(url);  // Clean up
</script>

âś… Supported File Types

  • PDF, DOCX, XLSX, PPTX (Office documents)
  • ZIP, RAR, TAR (archives)
  • Images (JPG, PNG, GIF, WebP)
  • Audio/Video (MP3, MP4, WAV)
  • Text (TXT, CSV, JSON, XML)
  • Generated content (blob: URLs)

đź’ˇ Limitations

  • Only works for same-origin URLs (or blobs)
  • Cross-origin URLs ignore download attribute (security)
  • Requires user interaction (can’t trigger programmatically without click)
  • Browsers may ignore if file type is commonly viewed (HTML)

“Users kept asking ‘How do I save this PDF?’ Added download attribute. Now they click, file downloads, support tickets dropped. One line of HTML solved it.”

— Web Developer

Related posts:

HTML5 button Defaults to submit (Surprise!)

HTML5 “Broken Autofill Styles” — Chrome’s Yellow Background Bug

How to use client-side function on checkbox or any asp.net component

Post Views: 5

Post navigation

CSS: Use Scroll Snap for Smooth, Snap-to-Section Scrolling
JavaScript: Use structuredClone() for True Deep Copy of Objects

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

June 2026
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« May    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)
  • Add Constraint to SQL Table to ensure email contains @ (580)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (579)
  • Average of all values in a column that are not zero in SQL (538)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (505)
  • Find numbers with more than two decimal places in SQL (454)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files
  • Ajax: Add Custom Headers to Fetch Requests
  • JavaScript: Use console.table to Display Arrays as Tables
  • HTML: Use Spellcheck Attribute to Enable Browser Spell Check
  • CSS: Use user-select to Prevent Text Selection
  • Windows 11: Use Snipping Tool for Instant Screenshots

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com