来自DataURL的Blob? [英] Blob from DataURL?

查看:105
本文介绍了来自DataURL的Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 FileReader readAsDataURL()我可以将任意数据转换为数据URL。有没有办法使用内置浏览器apis将数据URL转换回 Blob 实例?

Using FileReader's readAsDataURL() I can transform arbitrary data into a Data URL. Is there way to convert a Data URL back into a Blob instance using builtin browser apis?

推荐答案

用户Matt一年前提出了以下代码(如何将dataURL转换为javascript中的文件对象?)这可能会对你有所帮助

User Matt has proposed the following code a year ago ( How to convert dataURL to file object in javascript? ) which might help you

编辑:正如一些评论者报道的那样,BlobBuilder已被弃用了一段时间前。这是更新的代码:

As some commenters reported, BlobBuilder has been deprecated some time ago. This is the updated code:

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);

  // create a view into the buffer
  var ia = new Uint8Array(ab);

  // set the bytes of the buffer to the correct values
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }

  // write the ArrayBuffer to a blob, and you're done
  var blob = new Blob([ab], {type: mimeString});
  return blob;

}

这篇关于来自DataURL的Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆