javascript - 非法构造函数错误 : convert Base64 to Blob fails on cordova

标签 javascript android angularjs cordova blob

我想使用 AngularJS 将我的 Base64 图像转换为我的 cordova 应用程序项目中的 blob,但我不断收到 Illegal constructor 错误。我已经尝试了很多在线提供的解决方案,但似乎都没有用。任何帮助表示赞赏。

var imageElement = angular.element(document.querySelector('#profileImg'));
var imageURI = dataURIToBlobURI(imageElement.attr('src'));

 function dataURIToBlobURI(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);
    }

    var bb = new Blob([ab], {type: 'image/png'});
    return bb;
  }

我一直在此处收到错误 new Blob([ab], {type: 'image/png'}) 并且似乎不知道如何让它工作。仅当应用在 Android 或 iOS 中时才会发生,而不会在 Chrome 中查看时发生。

我尝试了以下方法,但都无济于事。

var bb = new Blob(ab);
var bb = new Blob([ab]);
var bb = new Blob(dataURI); 

谢谢

最佳答案

金斯莱!可能,您可以重现错误的设备实际上并不支持 Blob。实际上你可以使用两种方式:

首先,检查 polyfill或类似于解决您的问题的方法。它将允许您将 Blob 用作构造函数。

其次,除了 Blob,您还可以使用 BlobBuilder。下面的小例子,

var bb = new BlobBuilder();
bb.append('blob content');
var blob = bb.getBlob('text/plain');

关于javascript - 非法构造函数错误 : convert Base64 to Blob fails on cordova,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344420/

相关文章:

Underscore.js - OO : Get item at index? 有更好的 JS 库可以做到这一点吗?

javascript - 如何使用包含凭据的提取和 POST 请求发送数据?

javascript - 链接上的单击事件,但只有一个

javascript - 图像中的背景颜色(firefox)

javascript - 谷歌地图 API v3 : Remove Default start/end markers

android - 如何更改位图中某些像素的颜色android

android - 在循环执行中提供延迟

javascript - ng-模型值未定义

angularjs - 在 Angularjs 中添加/删除路由更改的 CSS 类

javascript - 使用 Angular 获取 jquery cookie