Filename of downloaded file in data:Application/octet-stream;

parbi picture parbi · Aug 5, 2011 · Viewed 65.6k times · Source

I am trying to download a file using data uri in following manner:

<input type="button"
  onclick="window.location.href='data:Application/octet-stream;content-disposition:attachment;filename=file.txt,${details}'"
  value="Download"/>

The problem is that the downloaded file is always named 'Unknown', whatever I try to use as filename. Is this the correct way to give the file a name ? or something else needs to be done ?

Answer

Ashraf Bashir picture Ashraf Bashir · Dec 17, 2012

Here's the solution, you just have to add a download attribute to anchor tag a with desired name

<a href="data:application/csv;charset=utf-8,Col1%2CCol2%2CCol3%0AVal1%2CVal2%2CVal3%0AVal11%2CVal22%2CVal33%0AVal111%2CVal222%2CVal333"
  download="somedata.csv">Example</a>

Another solution is to use JQuery/Javascript

Anchor's Download Property