我制作了一个简单的html表单来上传图片。 我正在尝试使用强大的中间件来处理我的图像。
var form = new formidable.IncomingForm();
var filename = req.body.pid + ".jpg";
//Formidable uploads to operating systems tmp dir by default
form.uploadDir = "/src/main/images"; //set upload directory
form.keepExtensions = true; //keep file extension
form.on('progress', function(bytesReceived, bytesExpected) {
if (bytesReceived > MAX_UPLOAD_SIZE) {
console.log('### ERROR: FILE TOO LARGE');
}
});
form.parse(req, function(err, fields, files) {
//Formidable changes the name of the uploaded file
fs.rename(files.fileUploaded.path, filename);
});
我怀疑如何正确处理文件的重命名,因为并非所有文件都是jpg文件。但我真的需要将文件重命名为标准名称。
另外,我想使用form.on限制文件大小。有更好的方法吗?