在邮递员中上传文件时,该文件未存储在目标文件夹中

时间:2018-09-11 04:22:38

标签: javascript node.js multer

var multer = require('multer');
var fs = require('fs');
const nodemailer = require('nodemailer');
const db = require('./db');
const Image = db.images;
const fileUploadModel = require('./../upload-model');
const path = require('path');

var upload = (req, res) => {
    console.log(req.files);
    console.log(req.files.file.name);
    var uploadDoc = req.files.file;
    var storage = multer.diskStorage({

        destination: function (req,files, cb) {
          cb(null, './temp')
        },
        filename: function (req,files, cb) {
            var datetimestamp = Date.now();
            cb(null, uploadDoc.originalname.split('.')[uploadDoc.originalname.split('.').length - 2] + '-' + datetimestamp + '.' + uploadDoc.originalname.split('.')[uploadDoc.originalname.split('.').length - 1]);

        }
      })

      var upload = multer({ storage: storage });
    var fileUpload = new fileUploadModel(req.files.file);
    fileUpload.save({
            type:uploadDoc.type,
            name:uploadDoc.name,
            data: fs.readFileSync('./temp/'+uploadDoc.name)
        }).then(image => {
            try{
                fs.writeFileSync('./temp-uploads/' + image.name, image.data);       

                res.json({'msg': 'File uploaded successfully!', 'file': uploadDoc.file});


            }catch(e){
                console.log(e);
                res.json({'err': e});
            }
        })


};

在临时文件夹中没有存储任何内容。在我读取时间显示错误的文件的地方:错误:没有:这样的文件或目录。

在此数据中:fs.readFileSync('./temp/'+uploadDoc.name)行仅会出错,因为在临时文件夹中没有存储文件。我不知道我在哪里出错了。

首先,这会导致问题req.file在我解决之后未定义。 现在,它导致目标文件夹中的错误。

0 个答案:

没有答案