How to download a blob file from Chrome iOS in Javascript?

Es Noguera picture Es Noguera · Mar 19, 2015 · Viewed 38.4k times · Source

How to download a blob file from Chrome iOS in Javascript ?

I'm working on download files (pdf, excel, txt, png) from iOS. iOS hasn't a file systems which is a problem for downloads. I create a code which download a blob file depending of the OS and the navigator if is required. It works nicely on desktop (lastest versions of Chrome and IE), Mobile Android (Chrome, native navigator) and iOS iPad2 (Safari).

Now, Chrome iOS supposed to be like Safari mobile, but the algorithm doesn't works, Chrome iOs download the file by opening in a new tab but the page is empty.

I create my own blob to create the downloadUrl.

This is a part of the download function .

    var URL = window.URL || window.webkitURL;
    var downloadUrl = URL.createObjectURL(iobBLOB);
    var newWindow = null;   

    if (typeof a.download === 'undefined') {
            var newWindow = null;
            newWindow = window.open(downloadUrl, '_blank');
            setTimeout(function() {
                newWindow.document.title = isbFilename;
            }, 10);
    }

More details

Debugging on iPad 2 and iPhone 4

Trying to download an excell, pdf, txt, and Png files.

The devices doesn't have file system.

Thanks for helping me... Tell me if you need more information it's my first question.

Answer

Es Noguera picture Es Noguera · Mar 26, 2015

After searching, this solution seems to works fine on ChromeiOS and Safari. I found it on this post "https://stackoverflow.com/questions/24485077/how-to-open-blob-url-on-chrome-ios?answertab=oldest#tab-top". This solved my problem:

//isbContentType i.e. 'text/plain'
//isbFilename i.e. 'This is a title'
 var reader = new FileReader();
    reader.onload = function(e) {
       var bdata = btoa(reader.result);
       var datauri = 'data:' + isbContentType + ';base64,' + bdata;
       window.open(datauri);
       newWindow = setTimeout(function() {
           newWindow.document.title = isbFilename;
       }, 10);
    };
    reader.readAsBinaryString(iobBLOB);

I believed there was a way to download the blob file, but it wasn't. Thanks anyway