Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a blob created with a base64, and I need to make this data downloadable as a pdf.

I created this snippet:

    var blob = new Blob([byte]);
    var link = document.createElement('a');

    link.href = window.URL.createObjectURL(blob);
    link.target = '_blank';
    var fileName = name + '.pdf';
    link.download = fileName;
    link.click();

It works on all the browsers, except than safari mobile on iOS.

The file gets actually downloaded, but its name is "unknown", then it can't be open since the extension gets lost.

The problem is that the download attribute lacks support on this browser and IE.

There are a lot of workarounds for IE, but I didn't find any for safari/iOS.

Do you know how can I download a blob got from a base64 (no XHR involved) in this browser?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
700 views
Welcome To Ask or Share your Answers For Others

1 Answer

I need to make this data downloadable as a pdf (...) in safari iOS

SHORT ANSWER: you can't. Due this bug is impossible to download the file on safari iOS


The alternative is to open the file on the browser with the proper mime type, so it can show its content (and the user can then manually download it if needed).

Make sure to pass mime type when creating the Blob. reference

var blob = new Blob([byte], {type: 'application/pdf'});

Lastly, I'd strongly suggest you to use FileSaver.js which that can handle most of the corner cases/multiple browser support for save (or in this case, open) a file in javascript.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...