使用节点和multer将图像上传到heroku不起作用 [英] uploading image to heroku using node and multer not work

查看:58
本文介绍了使用节点和multer将图像上传到heroku不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Node 后端将图像文件上传到 Heroku 并且我可以让它工作,同样的过程在本地主机测试中工作得很好,但是在将我的项目部署到 Heroku 并对其进行测试后,过程中出现错误并且文件不会上传

I am trying to upload image files to Heroku using Node backend and I can make it work, The same exact process work just fine on Localhost testing but after deploying my project to Heroku and testing it, there is an error in the process and the files wont upload

后端:

let storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './uploads')
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname)
    }
  })

const upload = multer({storage: storage})


router.post('/', upload.single('carImage') ,(req, res) => {

    console.log(req.file);    
    let todayArr = today.slice(0, 4);

})

正面:

  uploadImageToServer = (imagePath, fileName) => {
      const photo = {
        fieldname:'carImage',
        uri: imagePath,
        name: fileName,
        type: 'image/jpeg',
      };
      const data = new FormData();
      data.append('carImage', photo);
      const config = {
        method: 'POST',
        body: data,
      };
      return fetch("https://foo.herokuapp.com/uploadcarimage", config);
    }

      this.uploadImageToServer(this.state.path, `${this.state.car.plate.toString()}-${date.join('-')}.jpg`)
        .then(resp => resp.json())
        .then(json => console.log(json))
        .catch(err => console.log(err))

服务器错误:

 POST /uploadcarimage 500 2172.411 ms - 229
2018-05-28T11:39:53.045824+00:00 heroku[router]: at=info method=GET path="/6866866-Mon-May-28-2018.jpg" host=pure-journey-53428.herokuapp.com request_id=d6f6dfff-af19-4a6f-8f76-a0f14e3f812e fwd="12.34.567.890" dyno=web.1 connect=0ms service=2ms status=404 bytes=368 protocol=https

注意:

仅使用 return fetch("http://localhost:3000/uploadcarimage", config);

它工作得很好.

推荐答案

如果您上传的代码带有 images 文件夹为空,则不会在 Heroku 服务器中创建 images 文件夹.我通过在图像文件夹中添加一个图像或任何文件来上传我的代码,从而解决了这个问题.现在它工作正常:)

If you uploaded the code with the images folder is empty, then the images folder is not created in the Heroku server. I've solved this issue by just uploaded my code by adding one image or any file inside the images folder. Now it's working fine :)

注意:如果文件夹不存在,multer 无法创建文件夹,因此您需要先创建它.

Note: The multer can't create a folder if the folder doesn't exist, so you need create it first.

这篇关于使用节点和multer将图像上传到heroku不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆