【发布时间】:2019-07-19 20:25:17
【问题描述】:
根据How to convert the file to base64 in JavaScript?,我找到了一种将图像转换为基于javascript的base64的方法:
//My Converter Function
function getBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log("befor");
console.log(reader.result);
console.log("after");
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
我使用它:
getBase64(file).then(
data => (Image64bit = data)
似乎我得到了 64 位字符串但我每次都返回此错误: (注意没有“then”它不会返回任何东西)
zone.js:192 Uncaught TypeError: Cannot read property 'then' of 未定义 在 UploadFileAndGetUrl (doctors.js:14949) 在 HTMLButtonElement。 (医生.js:14703) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) 在 Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188) 在 ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [作为调用] (zone.js:496) 在调用任务(zone.js:1540) 在 HTMLButtonElement.globalZoneAwareCallback (zone.js:1566)
之前
医生.js:14992 数据:图像/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAH0CAMAAADynrlKAAACBFBMVEUAzG9U1pBn2Zskz3vV8+H////g9umH4K71/Pjr+fHJ8Nm97dE/04aj5sCW47d43aSw6sg1143j+e801owDzXHS9ubQ9uRm4 医生.js:14993
之后
我该如何解决这个错误?
【问题讨论】:
-
你的函数没有返回一个承诺,所以你不能在它上面调用
.then(),
标签: javascript angular