将base64图像上传到Firebase存储会引发神秘的Angular错误

时间:2018-08-23 18:14:39

标签: angular firebase firebase-storage

我正在尝试通过常规Firebase API将base64映像上传到Firebase存储。这是代码:

finalName = this.generateRandomName();
const storageRef = firebase.storage().ref(filePath);

console.log(filePath); // users/foouid
console.log(finalName); // foo.jpg
console.log(storageRef); // Reference {authWrapper: AuthWrapper, location: Location}
console.log(file); // data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQE ...

return storageRef.child(finalName).putString(file, 'base64', {contentType: 'image/jpeg'});

一切都记录正确的信息,但是由于某种原因在下一行中,Angular抛出以下错误,仅提及在视图中触发函数的按钮:

RegisterComponent.html:281
ERROR Error: [object Object]
    at viewWrappedDebugError (core.js:7595)
    at callWithDebugContext (core.js:11361)
    at Object.debugHandleEvent [as handleEvent] (core.js:11054)
    at dispatchEvent (core.js:7717)
    at core.js:8161
    at HTMLButtonElement.<anonymous> (platform-browser.js:995)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:3815)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)

此外,网络上没有发送http帖子。

1 个答案:

答案 0 :(得分:1)

结果证明Firebase Storage不喜欢字符串data:image/jpeg;base64,的开头,因为该方法已经可以解决了。从base64字符串中删除它就可以了。