How to set a file name using window.open

Asked
Active3 hr before
Viewed126 times

5 Answers

using
90%

Instead of using window.open() you could generate an invisible link with the download attribute and .click() it.,So when you click on download_1 id button then it will create an invisible download link and click that. I have used another function to prepare js.,You can achieve this using the download attribute for <a> elements. For example:, Is it can just open without download?? – shinriyo Jan 30 '19 at 6:16

You can achieve this using the download attribute for <a> elements. For example:

<a href="1251354216241621.txt" download="your-foo.txt">Download Your Foo</a>

Instead of using window.open() you could generate an invisible link with the download attribute and .click() it.

var str = "Name, Price\nApple, 2\nOrange, 3";
var uri = 'data:text/csv;charset=utf-8,' + str;

var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "data.csv";

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
load more v
88%

I'am trying to download temporary result calculated by JavaScript. Say I have a string str, I want to download a file contains str and named it as data.csv, I'm using the following code:,The file can be successfully downloaded, but how can I name the file data.csv automatically rather than set the name by hand each time?, javascript - use window.open How to stay on the current page , javascript - use window.open Multiple windows for ()

I'am trying to download temporary result calculated by JavaScript. Say I have a string str, I want to download a file contains str and named it as data.csv, I'm using the following code:

window.open('data:text/csv;charset=utf-8,' + str);
72%

<a onclick="window.open(document.URL, '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');">
   Open New Window
</a>
load more v
65%

Script-initiated windows opening automatically without the user's consent., Terms often used to describe or to identify the same window. It is the new window that was created. ,If this feature is on, the new secondary window will be resizable., windowFeatures parameter can specify the position and size of the new window.

var window = window.open(url, windowName, [windowFeatures]);
load more v
75%

Instead of using window.open() you could generate an invisible link with the download attribute and .click() it.

I recently stumbled upon this JSFiddle about how to convert a table to Excel file directly without any fancy plugins. It really suits my need, but it has a flaw, I can't rename its file. Chrome renames the file to download and Firefox gives a random name to it.

$("#btnExport").click(function(e) {
   window.title = "filename.xls"; // this part doesn't work
   window.open('data:application/vnd.ms-excel,' +
      $('#dvData').html());
   e.preventDefault();
});
load more v

Other "using-undefined" queries related to "How to set a file name using window.open"