【发布时间】:2020-04-17 03:01:03
【问题描述】:
搜索同一主题,我发现了另一个问题: How to Fetch and Display an Image from an express backend server to a React js frontend? 在 cmets 中有这样的:“将你的 blob 转换为 File var file = new File( res.data, { type: "image/jpeg" } ); var imageUrl = URL.createObjectURL(file); 然后使用这个 imageUrl ”。所以我做了类似于他的代码并得到了错误。
fetchImages = () => {
const imageName = 'Vinicius_0-1577392361334.jpg';
api.get(`/image/${imageName}`)
.then(res => {
var file = new File(res.data, { type: 'image/jpg' });
var imageURL = URL.createObjectURL(file);
return (
<Image source={imageURL} />
)
}).catch((err) => {
console.log(err)
});}
routes.get('/image/:file', (req,res) =>{
let file = req.params.file;
let fileLocation = path.join(__dirname, '..', '..', 'backend/uploads/', file);
res.sendFile(`${fileLocation}`);
});
.....
<View style={styles.viewBotao}>
{this.fetchImages()}
.....
【问题讨论】:
标签: javascript node.js reactjs react-native express