kennethjiang / js-file-download

MIT License
918 stars 119 forks source link

How to get file name from the server? #47

Closed Even-Lau closed 4 years ago

Even-Lau commented 5 years ago

How to get file name from the server?

Auskennfuchs commented 5 years ago

Normally the server should send the content with some Header information with the used filename. Afaik the standard header is Content-Disposition. So you can parse this header of the response and set the correct filename. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

jpauley-veem commented 5 years ago

I am using axios.get, and I can see the Content-Disposition in the response headers on the browser, but when I console.warn the response headers, it is removed. Any ideas about this?

jpauley-veem commented 5 years ago

I am using axios.get, and I can see the Content-Disposition in the response headers on the browser, but when I console.warn the response headers, it is removed. Any ideas about this?

Good news/bad news; turns out the service needs to expose this in order to be able to read it.

I would request an enhancement though, where if no filename is provided, it tries to replace with the value from headers.

cakidnyc commented 4 years ago

Here's how you can get the filename from the header (if it's specified): https://stackoverflow.com/questions/41812056/extract-both-json-and-headers-from-fetch

kindrowboat commented 4 years ago

@jpauley-veem that sounds like an interesting feature. However, js-file-download only takes the data to be saved, not the response object, so it wouldn't have access to the headers. Even if it did, there would be some edge cases to think about. For instance: what would be the fallback behaviour if js-file-download couldn't determine a filename from the header or the header isn't specified. Also, js-file-download doesn't need to be "downloading" data from a http request. The data can come from anywhere (e.g. websockets or even written up in the client-side JS/browser).

All-in-all, it seams to me to limit the concerns of js-file-download, it should be left up to the caller to determine the filename, either using the headers or otherwise. As the original question was answered, I'm going to close this issue. However, if you want to keep discussing this feature request, please feel free to open a new issue.