R.G*_*.G. 6 javascript file-upload node.js express
到目前为止,我使用expressjs大约一个月,我偶然发现了文件上传的问题.尽管咨询谷歌和各种博客,但我找不到以下三个问题的答案:
我需要做什么/ bodyParser的设置我必须选择以便:
确保确实上传了文件(目前,在没有选择文件的情况下提交表单时会创建一个空文件).
在哪里可以指定文件允许的最大大小值?
如何省略文件的重命名?
目前,我在Express(v.3.0.0)应用程序中包含bodyParser,其中包含以下选项:
{keepExtensions: true, uploadDir: __dirname + '/public/uploads'}
Run Code Online (Sandbox Code Playgroud)
我最近遇到了类似的问题。对于问题#2,请检查http://www.senchalabs.org/connect/middleware-limit.html
app.use(express.limit('4M')); // in your app.configure()
app.post('/upload', function(req, res) {
var fs = require('fs'),
file = req.files.myfile; //your file field;
if(file.size === 0) { // question #1
fs.unlinkSync(file.path);
res.redirect('/error?');
} else { //question #3
var fn = file.path.split('/');
fs.rename(file.path, file.path.replace(fn[fn.length-1], file.name));
res.redirect('/success?');
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2163 次 |
最近记录: |