如何在javascript中将dataURL转换为文件对象?

IT技术 javascript jquery fileapi
2021-02-08 15:02:37

我需要在 Javascript 中将 dataURL 转换为 File 对象,以便使用 AJAX 发送它。是否可以?如果是,请告诉我如何。

5个回答

如果您需要通过ajax发送它,那么就不需要使用File对象,只需要BlobFormData对象。

正如我旁注的那样,您为什么不通过 ajax 将 base64 字符串发送到服务器并将其转换为二进制服务器端,base64_decode例如使用 PHP 无论如何,这个答案中符合标准的代码适用于 Chrome 13 和 WebKit nightlies:

function dataURItoBlob(dataURI) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
    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);
    }

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

    //New Code
    return new Blob([ab], {type: mimeString});


}

然后只需将 blob 附加到一个新的 FormData 对象并使用 ajax 将其发布到您的服务器:

var blob = dataURItoBlob(someDataUrl);
var fd = new FormData(document.forms[0]);
var xhr = new XMLHttpRequest();

fd.append("myFile", blob);
xhr.open('POST', '/', true);
xhr.send(fd);
谢谢你的回答,不过,我已经做了类似的东西,而且效果很好。它是一个基于画布的图像调整器、水印和上传器。等待一个项目使用它
2021-03-17 15:02:37
BlobBuilder 现在已弃用,不再在 Chrome 中工作!
2021-03-29 15:02:37
值得一提的是,Base64编码的文件往往是大大低于实际的文件,这意味着更多的数据使用Base64时相比,传输较大的multipart/form-data,这是你当你使用File
2021-03-31 15:02:37
嗨,只是想提一下我在nixtu.info/2013/06/how-to-upload-canvas-data-to-server.html 上有一个稍微现代化的版本
2021-04-04 15:02:37
这篇文章使用了不推荐使用的元素,例如 BlobBuilder,请参阅@cuixiping 的答案以获得有效的解决方案。
2021-04-07 15:02:37

BlobBuilder已经过时,不应再使用。使用Blob而不是旧的 BlobBuilder。代码非常干净和简单。

File 对象继承自 Blob 对象。您可以将它们都与 FormData 对象一起使用。

function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type:mime});
}

使用 dataURLtoBlob() 函数将 dataURL 转换为 blob 并将 ajax 发送到服务器。

例如:

var dataurl = 'data:text/plain;base64,aGVsbG8gd29ybGQ=';
var blob = dataURLtoBlob(dataurl);
var fd = new FormData();
fd.append("file", blob, "hello.txt");
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server.php', true);
xhr.onload = function(){
    alert('upload complete');
};
xhr.send(fd);

其他方式:

您还可以使用fetch将 url 转换为文件对象(文件对象具有 name/fileName 属性,这与 blob 对象不同)

代码非常简短且易于使用。 (works in Chrome and Firefox)

//load src and convert to a File instance object
//work for any type of src, not only image src.
//return a promise that resolves with a File instance

function srcToFile(src, fileName, mimeType){
    return (fetch(src)
        .then(function(res){return res.arrayBuffer();})
        .then(function(buf){return new File([buf], fileName, {type:mimeType});})
    );
}

用法示例1:只需转换为文件对象

srcToFile(
    'data:text/plain;base64,aGVsbG8gd29ybGQ=',
    'hello.txt',
    'text/plain'
)
.then(function(file){
    console.log(file);
})

使用示例2:转换为文件对象并上传到服务器

srcToFile(
    'data:text/plain;base64,aGVsbG8gd29ybGQ=',
    'hello.txt',
    'text/plain'
)
.then(function(file){
    console.log(file);
    var fd = new FormData();
    fd.append("file", file);
    return fetch('/server.php', {method:'POST', body:fd});
})
.then(function(res){
    return res.text();
})
.then(console.log)
.catch(console.error)
;
爱它!最好的答案 imo
2021-03-22 15:02:37
如果没有Promise版本,这将如何?
2021-04-05 15:02:37
如果您没有查看 Mozilla Developer Network 参考,则 fetch 是一项实验性功能,不建议在生产代码中使用。可惜了,长得帅
2021-04-07 15:02:37
我真的很喜欢你的回答,因为它干净利落,但我真的可以在其他答案中找出 ArrayBuffer 的用法。你能解释一下 ArrayBuffer 的用法以及为什么这里不使用它吗?
2021-04-12 15:02:37

要从 dataURL 创建 blob:

const blob = await (await fetch(dataURL)).blob(); 

要从 blob 创建文件:

const file = new File([blob], 'fileName.jpg', {type:"image/jpeg", lastModified:new Date()});
不需要 2 次等待和嵌套。 await fetch(src).then(it => it.blob())
2021-03-23 15:02:37
非常感谢你!这个从 dataURL 创建 blob 的简洁解决方案非常有效:-)
2021-04-03 15:02:37

如果您真的想将 dataURL 转换为File对象。

您需要将 dataURL 转换为,Blob然后将其转换BlobFile. 该功能来自马修的回答。( https://stackoverflow.com/a/7261048/13647044 )

function dataURItoBlob(dataURI) {
      // convert base64 to raw binary data held in a string
      // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
      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);
      }
      return new Blob([ab], { type: mimeString });
    }

const blob = dataURItoBlob(url);
const resultFile = new File([blob], "file_name");

除此之外,您可以在File初始化对象上设置选项对 File() 构造函数的引用

const resultFile = new File([blob], "file_name",{type:file.type,lastModified:1597081051454});

类型应该是[MIME][1]type(ie image/jpeg) 并且在我的例子中最后修改的值相当于Mon Aug 10 2020 19:37:31 GMT+0200 (Eastern European Standard Time)

经过一番研究,我得到了这个:

function dataURItoBlob(dataURI) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
    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 dw = new DataView(ab);
    for(var i = 0; i < byteString.length; i++) {
        dw.setUint8(i, byteString.charCodeAt(i));
    }
    // write the ArrayBuffer to a blob, and you're done
    return new Blob([ab], {type: mimeString});
}

module.exports = dataURItoBlob;