obi*_*obi 7 javascript heroku node.js multer
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
BACKEND:
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);
})
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
SERVER ERROR:
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
Run Code Online (Sandbox Code Playgroud)
NOTE:
when trying this exact code only using return fetch("http://localhost:3000/uploadcarimage", config);
it work just fine.
小智 7
如果您上传的代码图像文件夹为空,则不会在 Heroku 服务器中创建图像文件夹。我通过在图像文件夹中添加一个图像或任何文件来上传我的代码,从而解决了这个问题。现在它工作正常:)
注意:如果文件夹不存在,multer 无法创建文件夹,因此您需要先创建它。
我不确定,但可能您需要设置目的地如下:
const path = require('path');
destination: function (req, file, cb) {
cb(null, path.resolve(__dirname, 'build'))
}
Run Code Online (Sandbox Code Playgroud)
就我而言,它解决了问题。
还需要记住,如果 multer 不存在,则无法创建文件夹,因此您需要先创建它,或者如果您将应用程序 fe 部署到 heroku 设置目标,如上所述可以提供帮助。
在任何情况下,您都可以使用下面的我的代码(在节点服务器上) - 它有效:
const multer = require('multer');
const path = require('path');
if (process.env.NODE_ENV === 'production') {
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.resolve(__dirname, 'build'))
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '_' + Date.now() + '_' + file.originalname)
}
})
} else {
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.resolve(__dirname, 'uploads'))
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '_' + Date.now() + '_' + file.originalname)
}
})
}
const uploads = multer({ storage: storage });
app.use(uploads.any());
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.resolve(__dirname, 'build')));
} else {
app.use(express.static('./public'));
}
//if you need to download (after upload) files in cloudinary
const cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: '...',
api_key: '...',
api_secret: '...'
});
//if you need to del files after upload
const fs = require('fs');
router.post('/admin/create-product-images', (req, res, next) => {
let urls = [];
async function sendImagesToCloudinary() {
for (let file of req.files) {
await cloudinary.uploader.upload(
file.path,
{
public_id: `${Date.now()}`,
resource_type: 'auto'
}
).then(result => {
//del files after upload on cloudinary
fs.unlink(file.path, function (err) {
if (err) {
console.log(err);
}
});
urls.push(result.url);
})
.catch(err => {
console.log(err);
});
}
res.json(urls);
}
sendImagesToCloudinary();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3150 次 |
| 最近记录: |