小智 5
我尝试了很多解决方案,但没有任何帮助。最后我写了这个,它有效!
我的解决方案(我使用 express 4.13 和 multer 1.2):
进口:
var express = require('express');
var router = express.Router();
var fs = require('fs');
var multer = require('multer');
Run Code Online (Sandbox Code Playgroud)
存储变量(请参阅此处的文档)
var storage = multer.diskStorage({
destination: function (req, file, cb) {
var newDestination = 'uploads/' + req.params.__something;
var stat = null;
try {
stat = fs.statSync(newDestination);
} catch (err) {
fs.mkdirSync(newDestination);
}
if (stat && !stat.isDirectory()) {
throw new Error('Directory cannot be created because an inode of a different type exists at "' + dest + '"');
}
cb(null, newDestination);
}
});
Run Code Online (Sandbox Code Playgroud)
初始化 Multer:
var upload = multer(
{
dest: 'uploads/',
limits: {
fieldNameSize: 100,
fileSize: 60000000
},
storage: storage
}
);
Run Code Online (Sandbox Code Playgroud)
使用它!
router.use("/upload", upload.single("obj"));
router.post('/upload', controllers.upload_file);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3649 次 |
| 最近记录: |