vue解析blob数据库类型报错
项目场景:
项目请求是需要上传文件处理之后还要下载文件的,但是难免报错,报错之后解析的也是blob类型。
问题描述
程序发生问题时,解析不了blob,报错:
Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer
-
项目代码:
const reader = new FileReader()
reader.readAsText(error.response.data, 'utf-8')
reader.onload = function (e) {
const errorMsg = reader.result
Notification.error({
title: errorMsg,
duration: 3000
})
}
项目结合了
element-ui的代码,error.response.data是一个Blob对象。
解决
其实blob对象有自己的解析方法的,可以直接使用text方法解析,但是这是一个异步对象Promise,所以需要在在后续方法中解析。
error.response.data.text().then(res => {
Notification.error({
title: res,
duration: 3000
})
})
总结
前端代码还是没那么熟悉,还是得好好研究!