我试图从 nodeJS 传递一个类型为“image/jpeg”的 blob 来做出反应。在nodejs端,我使用arraybuffer传递数据,在react端,我尝试使用res.blob检索它,它通常应该将数据转换回来。问题是 axios 返回一个错误,指出 res.blob() 不是函数。那么我如何在反应端检索它呢?任何回复将不胜感激,如果我没有充分描述我的问题,我深表歉意。
这是我在nodejs中的代码
res.type(blob.type);
blob.arrayBuffer().then((buf) => {
res.end(Buffer.from(buf));
});
这是我的反应代码。
axios
.post(imageUrl)
.then((res) => {
return res.blob();
})
.then((blob) => {
storageRef
.child(path + filename)
.put(blob)
.then(function (snapshot) {
return snapshot.ref.getDownloadURL();
})
使用 axios 时,不需要使用 blob(),只需将 responseType: 'blob' 设置为 axios 选项即可。
例如
axios.get(PF + userCreds.user.profilePic, {responseType: 'blob'})
然后在 .then() 函数中而不是
.then(axios.spread((...responses) => {
responses.map((res) => (
console.log(URL.createObjectURL(res.data.blob())
))
}))
do that
.then(axios.spread((...responses) => {
responses.map((res) => (
console.log(URL.createObjectURL(res.data))
))
}))
结果与fetch相同
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)