在目标文件夹中上传的文件不存储在nodejs中

问题描述 投票:-1回答:1

controller.js

var multer = require('multer');

var upload = (req, res) => {
    var doc = req.files.file;
    var storage = multer.diskStorage({
        destination:(req,files, cb)=> {
          cb(null, '/uploads')
        },
        filename:(req,files, cb) => {
            var datetimestamp = Date.now();
            cb(null, uploadDoc.originalname);

        }
      })

      var upload = multer({
        storage: multer.memoryStorage()
    }).single('file')


    upload(req, res, function (err) {
        if(err) {
            res.json({ error_code: 1, err_desc: err });
            return;
        }
        res.json({'msg': 'File uploaded successfully!', 'file': req.files.file});


    });
};

module.exports={ upload:upload}

router.js

var express =require("express");
var Router = express.Router();

var Controller = require('../Controllers/Ucontrollers');

Router.route('/upload/file').post(Controller.mediaUpload);

module.exports =Router;

在邮差显示结果:

{
    "msg": "File uploaded successfully!",
    "file": {
        "fieldName": "file",
        "originalFilename": "doc.pdf",
        "path": "C:\\Users\\DELL\\AppData\\Local\\Temp\\zwruODmEBT-iWFT0_zDJt8-y.pdf",
        "headers": {
            "content-disposition": "form-data; name=\"file\"; filename=\"doc.pdf\"",
            "content-type": "application/pdf"
        },
        "size": 292202,
        "name": "doc.pdf",
        "type": "application/pdf"
    }
}

但是在目标文件夹中没有文档内部。邮递员获取结果成功但在上传文件夹中没有存储。

我尝试了一切,但文档没有存储在目的地。我在这里错了。

javascript node.js multer
1个回答
0
投票

从响应中,您可以理解该文件存储在临时文件夹中。

{
    "msg": "File uploaded successfully!",
    "file": {
        "fieldName": "file",
        "originalFilename": "doc.pdf",
        "path": "C:\\Users\\DELL\\AppData\\Local\\Temp\\zwruODmEBT-iWFT0_zDJt8-y.pdf",
        "headers": {
            "content-disposition": "form-data; name=\"file\"; filename=\"doc.pdf\"",
            "content-type": "application/pdf"
        },
        "size": 292202,
        "name": "doc.pdf",
        "type": "application/pdf"
    }
}

问题是,您没有使用磁盘存储。只需在controller.js中进行以下更改即可

var upload = multer({
  storage: storage
}).single('file')
© www.soinside.com 2019 - 2024. All rights reserved.