我一直在使用node-stridable,它非常适合上传和播放。处理单个文件。到目前为止,这是我的代码:
var form = new formidable.IncomingForm(),
files = [],
fields = [];
form.uploadDir = __dirname+'/../public/content/images/';
var thumbnailDir = __dirname+'/../public/content/images/thumbnails/';
form
.on('field', function(field, value) {
fields.push([field, value]);
})
.on('file', function(field, file) {
files.push([field, file]);
})
.on('progress', function(bytesReceived, bytesExpected) {
var onePercent = (bytesExpected / 100);
var percentageUploaded = (bytesReceived / onePercent).toFixed(0) + '%';
console.log(percentageUploaded);
io.sockets.emit('uploadpercentage', { percentage: percentageUploaded });
})
.on('end', function() {
console.log('-> upload done');
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received fields:\n\n '+util.inspect(fields));
res.write('\n\n');
res.end('received files:\n\n '+util.inspect(files));
/* Go through each file and save it, then resize it and save thumbnail version */
files.forEach(function(file) {
/* Get the file contents, which are stored in the 2nd value of the array */
var fileContents = file[1];
/* Setup path variables */
var filePath = form.uploadDir + fileContents.name;
var thumbnailPath = thumbnailDir + fileContents.name;
/* Rename the image to the original filename */
fs.rename(fileContents.path, filePath, function() {
console.log('Its saved & renamed!');
/* Imagemagick - make thumbnails */
im.crop({
srcPath: filePath,
dstPath: thumbnailPath,
width: 150,
height: 150,
quality: 1
}, function(err, stdout, stderr) {
if (err) throw err;
});
}); /* End of rename function */
}); /* End of for statement */
});
/* Parse the form */
form.parse(req);
理想情况下,我想做的是能够拖拽和将目录放入files []输入,并使用Node.js处理该文件夹及其所有内容。这可能是强大的吗?如果是这样,它是如何完成的,如果没有,是否有任何替代方法我可以看到这样做?
如果我现在尝试上传文件夹,则会收到以下错误消息:
Error: MultipartParser.end(): stream ended unexpectedly: state = PART_DATA
感谢您的帮助