[英]Image upload functionality not working on deployed Heroku app but working on Localhost?
So I created my first big project: https://rate-n-write.herokuapp.com/所以我创建了我的第一个大项目: https://rate-n-write.herokuapp.com/
In brief, this is a blog app where the user can write reviews and publish them along with pictures.简而言之,这是一个博客应用程序,用户可以在其中撰写评论并将其与图片一起发布。
I have used firebase as the database to store the articles.我使用 firebase 作为数据库来存储文章。 The app is working fine on localhost.
该应用程序在本地主机上运行良好。 Whenever I am trying to upload an image on Heroku, I get this error
每当我尝试在 Heroku 上上传图像时,我都会收到此错误
The error is showing up in line number 8 of the following code (editor.js):该错误显示在以下代码 (editor.js) 的第 8 行:
uploadInput.addEventListener('change', () => {
uploadImage(uploadInput, "image");
})
const uploadImage = (uploadFile, uploadType) => {
const [file] = uploadFile.files;
if(file && file.type.includes("image")){
const formdata = new FormData();
formdata.append('image', file);
//Error shows up here in the fetch line
fetch('/upload', {
method: 'post',
body: formdata
}).then(res => res.json())
.then(data => {
if(uploadType == "image"){
addImage(data, file.name);
} else{
bannerPath = `${location.origin}/${data}`;
banner.style.backgroundImage = `url("${bannerPath}")`;
}
})
const change_text = document.getElementById("uploadban");
change_text.innerHTML = " ";
} else{
alert("upload Image only");
}
}
This is just a snippet of the whole editor.js file.这只是整个 editor.js 文件的一个片段。
Is it because I am trying to upload the file to the project directory?是因为我试图将文件上传到项目目录吗? (server.js snippet below):
(下面的 server.js 片段):
app.post('/upload', (req, res) => {
let file = req.files.image;
let date = new Date();
// image name
let imagename = date.getDate() + date.getTime() + file.name;
// image upload path
let path = 'public/uploads/' + imagename;
// create upload
file.mv(path, (err, result) => {
if(err){
throw err;
} else{
// our image upload path
res.json(`uploads/${imagename}`)
}
})
})
Do I need to use an online storage service like AWS S3?我是否需要使用像 AWS S3 这样的在线存储服务?
Heroku is not suitable for persistent storage of data, the uploaded pictures would be deleted after a while (when the dyno is restarted) read this . Heroku 不适合数据的持久化存储,上传的图片会在一段时间后被删除(当重新启动测功机时) 阅读此。
I would suggest using 3rd party object Storage services like cloudinary or AWS S3我建议使用第 3 方 object 存储服务,例如cloudinary或AWS S3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.