How to Set Name of File Downloaded from Browser

How to set name of file downloaded from browser?

Can't find a way in HTML. I think you'll need a server-side script which will output a content-disposition header. In php this is done like this:

header('Content-Disposition: attachment; filename="downloaded.pdf"');

if you wish to provide a default filename, but not automatic download, this seems to work.

header('Content-Disposition: inline; filename="filetodownload.jpg"');

In fact, it is the server that is directly serving your files, so you have no way to interact with it from HTML, as HTML is not involved at all.

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

Setting the name of a file downloaded from the browser

  1. If you are calling some Rails function like "send_file", then search the source code of your version of Rails to find the source code of that function and see what headers it sets. You have to follow the call stack down a couple of levels but you should be able to find out how it sets the headers; I have done this before. As for the browser, I think if it doesn't find a file name in the Content-Disposition header it will more or less use the last portion of the URL for a filename.
  2. Try using "inline" instead of "attachment" in the header.

File download - How can I control the filename AND respect the users preferences?

Chrome does expose this preference through the navigator.plugins dictionary. If this object contains a PDF Reader, then you know the user wants to see it displayed in their browser.

const opensInBrowser = [...navigator.plugins].some(plug => [...plug].some(mime => mime.type === 'application/pdf'))console.log(opensInBrowser);

How browser find out file name in downloads?

The do use content-disposition or any other standard method. See this example I just get from SourceForge using Live HTTP Headers:

HTTP/1.1 302 Found
X-Powered-By: PHP/5.2.9
Content-Disposition: attachment; filename="FunctionList_2_1_UNI_dll.zip"
Location: http://mesh.dl.sourceforge.net/project/npp-plugins/Function%20List/FunctionList%20Plugin%20v2.1/FunctionList_2_1_UNI_dll.zip
Content-Type: text/html
Content-Length: 0
Date: Sat, 13 Nov 2010 22:30:00 GMT
Server: lighttpd/1.4.26

Note that they also use a redirect, maybe you get confused by that.

Downloading a file with a different name to the stored name

Sure, use a Content-disposition header

header('Content-Disposition: attachment; filename="filetodownload.jpg"');

if you wish to provide a default filename, but not automatic download, this seems to work.

header('Content-Disposition: filename="filetodownload.jpg"');


Related Topics



Leave a reply



Submit