我遇到问题是上传带有multer的docx文件。
我宣称:
app.js multer config
var storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, __dirname + process.env.UPLOAD_DIR); // '/uploads'
}
});
var uploadService = multer({
storage: storage,
// limits: { fileSize: maxSize },
fileFilter: function(req, file, callback) {
var ext = path.extname(file.originalname)
if (ext !== '.docx') {
return callback(res.end('Only docx are allowed'), null)
}
callback(null, true)
}
});
app.post('/upload', uploadService.single('file'), uploadController.postFileUpload);
upload.js
exports.postFileUpload = (req, res, next) => {
// Data receiver parameters
var jsonData = '';
var buffer = generateDoc(jsonData, req.file);
[...]
res.status(200).end();
};
function generateDoc(data, file) {
var doc = new Docxtemplater();
var wordDocument = file;
var filepath = wordDocument.path;
console.log(filepath); // prints file path ex : D:\doc_tpl\uploads\0816cdb5c82d458ed930d0048b603d8a
var content = fs.readFileSync(path.resolve(filepath), 'binary'); // throws error, wtf ?
[...]
return buffer;
}
然后我发现了这个错误:
TypeError:无法读取属性'路径'未定义的 在generateDoc(D:\ doc_tpl \ controllers \ upload.js:17:31)
我无法使用var,正如您所看到的,通过另一个变量不会解决问题..
我使用Node@8.7,"表达":" ^ 4.15.4"," multer":" ^ 1.3。 0&#34 ;.而我的哈巴狗html身体似乎很干净:
form#file-uploader.dropzone(method='post', action='/upload' enctype='multipart/form-data')
.fallback
input(name='file' type='file')
这段代码有什么问题?