【发布时间】:2018-11-07 01:00:21
【问题描述】:
我正在尝试使用 Node 后端将图像文件上传到 Heroku,我可以让它工作,相同的过程在 Localhost 测试中工作得很好,但是在将我的项目部署到 Heroku 并对其进行测试后,过程中出现了错误而且文件不会上传
后端:
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); 尝试此确切代码时
它工作得很好。
【问题讨论】:
标签: javascript node.js heroku multer