Download blobs locally using Safari

后端 未结 6 1680
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 21:36

I\'m trying to find a cross browser way to store data locally in HTML5. I have generated a chunk of data in a Blob (see MDN). Now I want to move this Blob to the actual file

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 22:05

    Here data is the array buffer data coming from response while making http rest call in js. This works in safari, however there might me some issue in filename as it comes to be untitled.

    var binary = '';
    var bytes = new Uint8Array(data);
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
    }
    
    var base64 = 'data:' + contentType + ';base64,' + window.btoa(binary);
    var uri = encodeURI(base64);
    var anchor = document.createElement('a');
    document.body.appendChild(anchor);
    anchor.href = uri;
    anchor.download = fileName;
    anchor.click();
    document.body.removeChild(anchor);
    

提交回复
热议问题