Create a download link from a Blob URL

前端 未结 2 1662
一生所求
一生所求 2021-02-03 12:17

In a Google chrome extension I am working on, a file is downloaded from a server with an XMLHttpRequest. This file contains some binary data which are stored in an

2条回答
  •  伪装坚强ぢ
    2021-02-03 12:56

    I have never tried it before, but it should be possible to create a new File object (which allows you to specify a file name) and write your blob to it. Something along the lines of:

    function publish(data, filename) {
    
        if (!window.BlobBuilder && window.WebKitBlobBuilder) {
            window.BlobBuilder = window.WebKitBlobBuilder;
        }
    
        fs.root.getFile(filename, {
            create: true
        }, function (fileEntry) {
    
            // Create a FileWriter object for our FileEntry (log.txt).
            fileEntry.createWriter(function (fileWriter) {
    
                fileWriter.onwriteend = function (e) {
                    console.log('Write completed.');
                };
    
                fileWriter.onerror = function (e) {
                    console.log('Write failed: ' + e.toString());
                };
    
                var builder = new BlobBuilder();
                builder.append(data);
                var blob = builder.getBlob();
                fileWriter.write(blob);
    
            }, errorHandler);
    
        }, errorHandler);
    }
    

    I think this could work for you.

提交回复
热议问题