I Downloaded file as response of ajax. How to get file name and file type from content-disposition and display thumbnail for it. i got many search results but couldn't find right way.
$(".download_btn").click(function () {
var uiid = $(this).data("id2");
$.ajax({
url: "http://localhost:8080/prj/" + data + "/" + uiid + "/getfile",
type: "GET",
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
},
success: function (response, status, xhr)
{
var header = xhr.getResponseHeader('Content-Disposition');
console.log(header);
}
});
console output:inline; filename=demo3.png
There is an npm package that does the job: content-disposition
Try this solution:
Didn't find any response on stackoverflow in my case. I receive a content-disposition like that: inline; filename*=UTF-8''Dossier%20de%20diagnostic%20technique.pdf
I modified the regex to deal with an UTF-8 string if present : var regex = /filename[^;=\n]=(UTF-8(['"]))?(.*)/;
And here is my function :
EDIT : I didn't test it that much. Don't forget to wrap it with a try / catch in a NodeJS code.
Here is how I used it sometime back. I'm assuming you are providing the attachment as a server response.
I set the response header like this from my REST service
response.setHeader("Content-Disposition", "attachment;filename=XYZ.csv");
EDIT: Editing the answer to suit your question- use of the word
inline
instead ofattachment
More here