简体   繁体   English

multer 文件上传在服务器上与 express 挂起,但不在本地

[英]multer file upload hangs with express on server but not locally

I have been searching a while but not got answer please help我已经搜索了一段时间但没有得到答案请帮助

So I want to upload files to server, I have code所以我想将文件上传到服务器,我有代码

var upload = multer({ storage: common.storage , fileFilter : common.fileFilter,limits:{fileSize:config.maxSize} }).array('media');

upload(req, res, function (err) {

    if (err) {

        res.json({'success' : false , 'err' : err ,  msg : 'Something went wrong please try again'});
    }else{

        if(req.fileValidationError) {

            return res.end(req.fileValidationError);
        }`

Its some code to upload file on multer where common.storage function is as它的一些代码在 multer 上上传文件,其中 common.storage 功能是

'storage' : multer.diskStorage({
  destination: function (req, file, cb) {

     console.log(req.pathToSaveFile)
     cb(null, 'uploads/' + req.pathToSaveFile)
  },
  filename: function (req, file, cb) {
     console.log('file' , file)
     cb(null, req.timestamp +'_'+ file.originalname)
  },

}),

after console file nothing happens, please help me in this控制台文件后没有任何反应,请帮助我

` `

If your server is running with PM2 than its the most common error we got, because of watch here is an example for file upload with node js.如果您的服务器使用 PM2 运行而不是我们得到的最常见错误,因为 watch 这里是使用节点 js 上传文件的示例。

const multer = require('multer');
var storage =   multer.diskStorage({server
  destination: function (req, file, callback) {
    callback(null, 'UPLOAD_FOLDER_PATH');
  },
  filename: function (req, file, callback) {
    callback(null, new Date().toISOString().replace(/[-T:\.Z]/g, "") + file.originalname) ; // toISOString has been used to rename your file.
  }
});

var upload = multer({ storage : storage}).single('YOUR_INPUT_FIELD_NAME');
upload(req,res,function(err) {
  if(err) {
    return res.end("Error uploading file.");
  } else {
    // File uploaded successfully.
    // Do you stuff.
  }

Note : There are two way to use for the following code with PM2注意:以下代码有两种方法可以使用 PM2

1) Start PM2 server without watch. 1)不带手表启动PM2服务器。 (Not recommended.) (不推荐。)

pm2 start index.js pm2 启动 index.js

2) Start PM2 server with watch and ignore file upload folder. 2) 用 watch 启动 PM2 服务器并忽略文件上传文件夹。

pm2 start index.js --watch --ignore-watch "UPLOAD_FOLDER_PATH" pm2 启动 index.js --watch --ignore-watch "UPLOAD_FOLDER_PATH"

Happy coding cheers!快乐编码欢呼!

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

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