HTML5二进制文件写作与Base64

时间:2011-10-13 21:35:57

标签: javascript html5 google-chrome blob fileapi

如您所知,HTML 5提供了一个很好的FileAPI。我已经构建了一个系统,用户收到Base64编码的字符串,需要写在磁盘上(具有适当的权限,因为它是谷歌Chrome应用程序)。我的代码如下:(稍微清除了不必要的东西)

function onInitFs(fs){      
  fs.root.getFile(fileName, {create: true}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      var bb = new window.WebKitBlobBuilder(); 
      bb.append( atob(dataInBase64Format));
      fileWriter.write(bb.getBlob());           
    }, errorHandler);
    console.log( fileEntry.toURL());
  }, errorHandler);
}

但是,我的原始文件大小为:6302446字节,服务器以8403264字节将其作为Base64发送,但保存的文件为9242715字节。当然我明白了什么是错的,我查看了文件,它只是一个很好的字符串。没有花哨的字符出现。我假设我在文本模式下写,atob只是将它转换为另一个字符串;我需要转换为二进制格式(在一个数组中?)并强制我的fileWriter以二进制模式而不是文本模式写入。我在网上搜索过,但找不到解决方案。我在StackOverflow上发现了这个问题Are Google Chrome Base64 methods capable of handling binary data from the File API?,但它对我没有帮助。

如何将我的Base64字符串转换为正确的数据结构并让我的fileWriter编写它?

1 个答案:

答案 0 :(得分:8)

我编写了一个扩展程序,使用Chrome捕获屏幕截图,将其放在画布上,调整大小,然后使用Filesystem API保存画布数据。不确定这是否与你的直接相似,但也许大部分代码都足够了?

在这种情况下,我认为我的dataURI(例如myCanvas.toDataURL("image/png"))与您的dataInBase64Format的Base64格式相同。

功能:

// canvas.toBlob is not implemented in Chrome yet! So we have to build the blob ourselves.
// Derived from http://mustachified.com/master.js
// via http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-April/031243.html
// via https://bugs.webkit.org/show_bug.cgi?id=51652
// via http://code.google.com/p/chromium/issues/detail?id=67587

function dataURItoBlob(dataURI, callback) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs
    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to an ArrayBuffer
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var bb = new window.WebKitBlobBuilder();
    bb.append(ab);
    return bb.getBlob(mimeString);
}

用法:

// Save image data
function onInitFs(fs){
    fs.root.getFile(fileName, {create:true}, function(fileEntry) {
        fileEntry.createWriter(function(fileWriter) {
            fileWriter.write(dataURItoBlob(myCanvas.toDataURL("image/png")));
        }, fileErrorHandler);
    }, fileErrorHandler);
}, fileErrorHandler);