Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for 'download' attribute in Safari/iOS

Tags:

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 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

like image 770
Cristian Traìna Avatar asked Apr 19 '19 10:04

Cristian Traìna


People also ask

How do I download files from Safari iOS?

To download a file in Safari, just tap on a download link on a website or tap and hold on a link and then tap Download Linked File (Figure B). Downloading a file in Safari on iOS or iPadOS is as easy as tapping and holding on a link and selecting Download Linked File.

How do I download a file from a website to my Iphone?

Go to a web page and find the link for a file you want to download. When you select it, you'll see a popup with the filename asking if you want to download it. Tap on the “Download” button. The download will start, and you'll see a new “Downloads” button appear next to the address bar at the top of the browser.

Why download attribute is not working in HTML?

The download attribute only works for same-originl URLs. So if the href is not the same origin as the site, it won't work. In other words, you can only download files that belongs to that website. This attribute follows the same rules outline in the same-origin policy.


2 Answers

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.

like image 156
Diogo Sgrillo Avatar answered Oct 01 '22 03:10

Diogo Sgrillo


As per the below link:-

https://caniuse.com/#feat=download

Safari 13 Beta 3 is released so you can check on the same, whether its working or not?

You can download a blob got from a base64 by using a atob function.

The atob function will decode a base64-encoded string into a new string with a character for each byte of the binary data.

You can save blob locally via FileSaver.js .

You can also check here that would be helpful:- How to open Blob URL on Chrome iOS

like image 39
Parveen yadav Avatar answered Oct 01 '22 03:10

Parveen yadav