我正在尝试使用 Node 后端将图像文件上传到 Heroku,我可以使其工作,同样的过程在本地主机测试中工作得很好,但是在将我的项目部署到 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);
})
FRONT:
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
NOTE:
当尝试这个确切的代码时仅使用return fetch("http://localhost:3000/uploadcarimage", config);
它工作得很好。