hh5*_*188 6 file-upload node.js express formidable
其实我遇到了两个问题
首先,如何更改上传路径
我的文件夹结构是这样的:
|__app.js
|__upload
Run Code Online (Sandbox Code Playgroud)
我的节点代码在app.js中并从中启动,所以我想上传图片上传到上传文件夹,我改变路径:
var form = new formidable.IncomingForm;
form.uploadDir = "./upload";
Run Code Online (Sandbox Code Playgroud)
它似乎上传成功,但我不知道文件去哪里,它不在上传文件夹中.
那么正确的路径名称是什么?
第二个问题是
如果我不更改它,它可以正确上传到它C:/Users/ADMINI~1/AppData/Local/Temp
但它将在没有foramt的情况下重命名,
那么如何才能获得上传格式并自行重命名?
第三个问题是
我还将处理程序绑定到process事件,例如
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(bytesReceived + ' ' + bytesExpected);
});
Run Code Online (Sandbox Code Playgroud)
但似乎不起作用,上传日志时没什么.为什么?我错过了什么吗?
这是我的所有代码:
app.post('/upload', function (req, res) {
var form = new formidable.IncomingForm;
// form.uploadDir = "./upload";
console.log(form.uploadDir);
form.parse(req, function(err, fields, files){
if (err) return res.end('You found error');
console.log(files.image);
});
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(bytesReceived + ' ' + bytesExpected);
});
form.on('error', function(err) {
res.writeHead(200, {'content-type': 'text/plain'});
res.end('error:\n\n'+util.inspect(err));
});
// res.end('Done');
res.send("well done");
return;
})
Run Code Online (Sandbox Code Playgroud)
首先,您必须告诉您的应用程序您不希望bodyParser处理文件上载.
app.use(express.bodyParser());
Run Code Online (Sandbox Code Playgroud)
是等同于
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());
Run Code Online (Sandbox Code Playgroud)
删除最后一行以自行处理文件上传.初始化表单时添加一些自定义选项
var form = new formidable.IncomingForm({
uploadDir: __dirname + '/tmp', // don't forget the __dirname here
keepExtensions: true
});
Run Code Online (Sandbox Code Playgroud)
现在你的代码应该工作了.
| 归档时间: |
|
| 查看次数: |
5622 次 |
| 最近记录: |