Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

javascript - Alternative for 'download' attribute in Safari/iOS

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...