简体   繁体   English

multer node.js 上传图片出现 500 Internet 服务器错误

[英]multer node.js upload images got a 500 internet server error

I tried to upload image file to node.js but got a 500 internal server error.我尝试将图像文件上传到 node.js,但出现 500 内部服务器错误。 This is my server side code.这是我的服务器端代码。

var multer = require('multer');
var storage = multer.diskStorage({
        destination: './public/uploads',
        filename: function (req, file, cb) {
            switch (file.mimetype) {
                case 'image/jpeg':
                    ext = '.jpeg';
                    break;
                case 'image/png':
                    ext = '.png';
                    break;
            }
            cb(null, file.originalname + ext);
        }
    });

var upload = multer({storage: storage});

router.post('/upload_photo', upload.single('photo'), function(req, res, next) {

    //save filename to db
    res.sendStatus(200);
});

On cilent side I simply use ajax like this在 cilent 方面,我只是像这样使用 ajax

$.ajax({
        url: '/upload_photo',
        type: 'POST',
        data: formData,
        contentType: false,
        cache: false,
        processData: false
    })

What's wrong here?这里有什么问题?

I used postman for testing the api and I came to know about the error In my case it was "path does not exists error" since I didn't not created that destination folder in which I am storing the images我使用邮递员来测试 api 并且我开始了解错误在我的情况下它是“路径不存在错误”,因为我没有创建我存储图像的目标文件夹

For eg:-例如:-

destination: './public/uploads',

In my case there is no folder name uploads inside public that's why I got server error 500在我的情况下,public 中没有文件夹名称上传,这就是为什么我收到服务器错误 500

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM