27

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 ?

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
parbi
  • 533
  • 1
  • 10
  • 19
  • So i think its not possible to give filename using dta-uri. Can it be done through javascript?? – parbi Aug 08 '11 at 10:15

5 Answers5

44

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

Zombo
  • 1
  • 62
  • 391
  • 407
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
6

On Safari, you might want to use this, and instruct the user to ⌘-S the file:

window.open('data:text/csv;base64,' + encodeURI($window.btoa(content)));

Otherwise, this uses Filesaver.js, but works ok:

    var downloadFile = function downloadFile(content, filename) {
      var supportsDownloadAttribute = 'download' in document.createElement('a');

      if(supportsDownloadAttribute) {
        var link = angular.element('<a/>');
        link.attr({
          href: 'data:attachment/csv;base64,' + encodeURI($window.btoa(content)),
          target: '_blank',
          download: filename
        })[0].click();
        $timeout(function() {
          link.remove();
        }, 50);
      } else if(typeof safari !== 'undefined') {
        window.open('data:attachment/csv;charset=utf-8,' + encodeURI(content));
      } else {
        var blob = new Blob([content], {type: "text/plain;charset=utf-8"});
        saveAs(blob, filename);
      }
    }

Note: There is some AngularJS in the code above, but it should be easy to factor out...

malix
  • 3,566
  • 1
  • 31
  • 41
2

I had the same issue and finally I solved in all browsers serving the CSV file in the server-side:

const result = json2csv({ data });

res.writeHead(200
        'Content-Type': 'application/octet-stream',
        'Content-Disposition': 'attachment;filename=issues.csv',
        'Content-Length': result.length
});

res.end(result);
Aral Roca
  • 5,442
  • 8
  • 47
  • 78
1

For those that are using other libraries like angularjs or backbone, you can try something like this.

$('a.download').attr('href', 'data:application/csv;charset=utf-8,'+$scope.data);

Rick
  • 12,606
  • 2
  • 43
  • 41
1

For anybody looking for a client-side solution using Javascript only, here is mine, working on any browser except IE 10 and lower (and Edge...why?!):

var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(csv);
var link = document.createElement('a');
link.setAttribute("download", "extract.csv");
link.setAttribute("href", uri);
document.body.appendChild(link);
link.click();
body.removeChild(body.lastChild);
Matt Croak
  • 2,788
  • 2
  • 17
  • 35
Synedh
  • 11
  • 2