How to open Blob URL on Chrome iOS
Asked Answered
P

4

23

I'd like to open Blob object in a browser window.

This code works everywhere but iOS Chrome (and IE of course but I can solve IE). The window is not redirected to the url (which is correct or at least the same as in other browsers). Is there any known workaround for Chrome iOS?

var blob = new window.Blob(['Hello, world!'], {type: 'text/plain;charset=utf-8'});
window.URL = window.URL || window.webkitURL;
var url = window.URL.createObjectURL(blob);
window.location.href = url;

I've tried <a href="{blobUrl}> instead of window.location.href but it doesn't work either.

Precocious answered 30/6, 2014 at 7:36 Comment(0)
P
37

FileReader solved my problem.

var reader = new FileReader();
var out = new Blob([this.response], {type: 'application/pdf'});
reader.onload = function(e){
  window.location.href = reader.result;
}
reader.readAsDataURL(out);
Precocious answered 2/7, 2014 at 12:30 Comment(4)
For some reason, this works for PDF for me in Chrome IOS, but not for epub, where epub's application type is: blob = new Blob([oReq.response], {type: 'application/epub+zip'}); The code works for both types in Safari, just not chrome. :(Krohn
oh my... this part of my life is called HappinessTreiber
Hi all, I need to manage an archive file in Chrome IOS, is it possible? I tryied to create a blob like new Blob([........], {type: 'application/zip'}); but the code don't work.Kalb
is it possible to open file in new window for IOS ?Arachne
Y
8

FileReader.readAsBinaryString method has been deprecated.

Bit late, but I had todo something similar using the FileReader.readAsDataURL - which produces a dataUrl. I'm using AngularJS $http service to call the API to create a pdf. Hope this helps, see below:

$http.post('/api/pdf', {id: '123'}, {responseType: 'arraybuffer'})
    .success(function (response) {
        var blob = new Blob([response.data], {type: 'application/pdf'});
        var reader = new FileReader();
        reader.onloadend = function(e) {
             $window.open(reader.result);
        }
        reader.readAsDataURL(blob);
    });
Yehudit answered 8/9, 2015 at 15:47 Comment(2)
Hi, Keano Your solutions is working fine with Firefox, but when I tried with Chrome it doesn't seems to be working. It just shows the url but the pdf is not loaded. Can you please guide me with this? Thanks in advance.Epizoic
Hi, I use the FileReader.readAsDataURL with PDF stream fine with Firefox and Chrome. Since few days or weeks, users report me it does not work anymore with Chrome or Vivaldi (same engine) but still working on Firefox. Maybe a regression on Chromium ?Costin
F
1

None of these solutions worked for me on Safari. I had to create a link on the page.

const downloadBlob = (fileName, blob) => {
  const link = document.createElement('a');
  link.href = URL.createObjectURL(blob);
  link.target = '_blank';
  link.download = fileName;
  document.body.appendChild(link);
  link.click();
}

const blob = new Blob([data], { type: 'video/mp4' });
downloadBlob('myfile.mp4', blob)
Freshman answered 21/1, 2021 at 2:58 Comment(0)
T
0

I fixed it by changing content-type from application/json to application/octet-stream. My xlsx file was downloaded as json.

@PostMapping(value = "/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)

Tammitammie answered 5/3, 2020 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.