简体   繁体   English

图像上传功能在部署的 Heroku 应用程序上不起作用,但在 Localhost 上起作用?

[英]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 上上传图像时,我都会收到此错误

1

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 存储服务,例如cloudinaryAWS S3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点应用程序可以在localhost上运行,但不能在Heroku上运行吗? - Node app working on localhost but not on Heroku? 图像上传功能在Angle 6中不起作用 - Image upload functionality not working in angular 6 我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误。 你可以阅读下面的描述 - my node app is working fine on localhost but when i deployed on heroku , it is giving me application error. you can read description below 为什么 fs 写文件在 localhost 中工作但在 heroku 应用程序中不起作用? - Why fs write file working in localhost but not working in heroku app? 工具提示可在localhost上使用,但不能在已部署的站点上使用 - Tooltip working in localhost but not on a deployed site 部署到heroku时投票系统无法正常工作 - voting system not working when deployed to heroku 在Heroku中部署CSS和JS后无法正常工作 - CSS and JS not working after deployed in heroku 将应用程序部署到Heroku时,Express JS,req.params命令不起作用 - Express JS, req.params command not working when app deployed to Heroku 如何获得静态图像路径在本地主机上的Webpack 2 / React App中工作? - How to get static image paths working in Webpack 2 / React App on localhost? SocketIO在Heroku环境中不起作用[在Localhost中工作] - SocketIO is not working in Heroku Environment [Works in Localhost]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM