2

如您所知,HTML 5 提供了一个不错的 FileAPI。我建立了一个系统,用户接收 Base64 编码的字符串,需要写入磁盘(具有适当的权限,因为它是 Google 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 字节,服务器将它们作为 Base64 以 8403264 字节发送,但是保存的文件是 9242715 字节。当然,我知道出了点问题,我查看了文件,它只是一个不错的字符串。没有花哨的字符出现。我假设我以文本模式编写,而 atob 只是将其转换为另一个字符串;我需要将其转换为二进制格式(也许在数组中?)并强制我的 fileWriter 以二进制模式而不是文本模式写入。我在网上搜索过,但找不到解决方案。我在 StackOverflow 上发现了这个问题Google Chrome Base64 方法是否能够处理来自 File API 的二进制数据?但这对我没有帮助。

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

4

1 回答 1

9

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

在这种情况下,我假设我的dataURI(例如myCanvas.toDataURL("image/png"))与您的dataInBase64Format.

功能:

// 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);
于 2011-10-15T01:09:51.237 回答