Change Name of Download in JavaScript

Change name of download in javascript

No, you cannot change this from the client side (HTML or javascript). You need to change it from the server. One way is to use a server side script which will set the Content-Disposition HTTP response header:

Content-Disposition: attachment; filename=somecustomname.txt

How to change the name of a file before it downloads in Puppeteer?

If the URLs of the CSV files can be obtained, you could insert (or modify) the download attribute of the <a> tag.

Otherwise, you could possibly intercept the server's HTTP response and modify the Content-Disposition response header to set a filename so the browser receives an altered HTTP response with the explicit filename.

How to rename or keep the file name only while downloading the file entering a required key?

There are multiple ways to solve your problem. The most simple solution is to set the download attribute without value

link.download = '';

This will use the final segment of the URL as filename.

download

Causes the browser to treat the linked URL as a download. Can be used with or without a value:

Without a value, the browser will suggest a filename/extension, generated from various sources:

  • The Content-Disposition HTTP header
  • The final segment in the URL path
  • The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)

Defining a value suggests it as the filename. / and \ characters are converted to underscores (_). Filesystems may forbid
other characters in filenames, so browsers will adjust the suggested
name if necessary.

MDN

Changing the name of an html download

Solved in duplicate question.

Content-Disposition: attachment; filename=somecustomname.txt

Change downloaded file name

The following function works fine:

export function download(url: string, filename: string) {
var a = document.createElement('a');
if (a.click) {
// Use a.click() if available. Otherwise, Chrome might show
// "Unsafe JavaScript attempt to initiate a navigation change
// for frame with URL" and not open the PDF at all.
// Supported by (not mentioned = untested):
// - Firefox 6 - 19 (4- does not support a.click, 5 ignores a.click)
// - Chrome 19 - 26 (18- does not support a.click)
// - Opera 9 - 12.15
// - Internet Explorer 6 - 10
// - Safari 6 (5.1- does not support a.click)
a.href = url;
a.target = '_parent';
// Use a.download if available. This increases the likelihood that
// the file is downloaded instead of opened by another PDF plugin.
if ('download' in a) {
a.download = filename;
}
// <a> must be in the document for IE and recent Firefox versions.
// (otherwise .click() is ignored)
(document.body || document.documentElement).appendChild(a);
a.click();
a.parentNode!.removeChild(a);
} else {
if (window.top === window &&
url.split('#')[0] === window.location.href.split('#')[0]) {
// If _parent == self, then opening an identical URL with different
// location hash will only cause a navigation, not a download.
var padCharacter = url.indexOf('?') === -1 ? '?' : '&';
url = url.replace(/#|$/, padCharacter + '$&');
}
window.open(url, '_parent');
}
}

Source

From PDF viewer : https://github.com/mozilla/pdf.js/blob/94089960c04d7f59eb637d6dc63944115bbc539d/web/download_manager.js#L29-L63



Related Topics



Leave a reply



Submit