私は node-formidable で遊んでいますが、単一ファイルのアップロードと処理に最適です。これまでの私のコードは次のとおりです。
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
助けてくれてありがとう