0
votes

Im getting a pdf back from a databse as a blob object and want to display the pdf back to the browser with a file name.

I get the file back no problem and able to display in new tab, but file name looks like --> 64CB13D-ec93-48fa-a425-0b66n3fg

How can i force a file name?

function(response){
    if(response.data && response.status==200){
        var fileContent = response.data;
        var file = new Blob([fileContent], {type: 'application/pdf'});
        var fileURL = URL.createObjectURL(file);
        window.open(fileURL, '_blank');
1
you can convert your blob into a File object, with both blob and filename in the constructor, then do the .createObjectURL() to that file. should work. developer.mozilla.org/en/docs/Web/API/FileStavm
@Stavm, care to share a code sample?Doc Holiday

1 Answers

-1
votes

I think you can just use HTML for this purpose, see my answer here: Display PDF stream returned from controller using jquery in new window

And add, server-side, the following header:

Content-Disposition: attachment; filename="the-name-you-want.pdf"

See: https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Disposition