dropzone nodejsfile upload

我打算使用DropZone将file upload到我的nodejs服务器。 但是,我不知道如何得到一个文件句柄,当这个post命中我的nodejs服务器。 下面是我如何在我的html中实例化DropZone:

<form action="/file-upload" class="dropzone" id="logoDropZone"> <input type="hidden" name="userName" value="user"/> </form> 

然后,在我的nodejs服务器,我有以下代码:

 app.post('/file-upload', function (request, response) { console.log("Route: '/file-upload' "); console.log("File upload request from user: " + request.body.userName); // Get a file handle, read the file and then write it out to the file system. ... }); 

我可以看到这个代码正在执行。 我也可以通过请求对象访问input字段中的值(参见隐藏的input)。 但是,如何访问文件本身,以便将其写入服务器文件系统?

我find了这个问题的答案。 以下代码将file upload到服务器:

 app.post('/file-upload', function (request, response) { fs.readFile(request.files.file.path, function(err, data) { var newPath = __dirname + "/public/img/xspectra/customlogo.png"; fs.writeFile(newPath, data, function (err) { console.log("Finished writing file..." + err); response.redirect("back"); }); }); }); 

所以别人不会卡在这个相同的问题上,我会解释我没有做的正确:

1)虽然表单元素没有“name”属性,但是可以通过名称“file”在节点请求对象中访问文件。 这可以在上面的代码中看到:

 request.files.file.path 

其中:“文件”是表单的“名称”。

2)这个代码是在下面的网站find的(在下面提供信用和提供build设性的批评): http : //howtonode.org/af136c8ce966618cc0857dbc5e5da01e9d4d87d5/really-simple-file-uploads

虽然我非常感谢创build文档的所有者,但是可以使用一些改进来减less混淆。 主要是文档需要更新,以明确解释上面的点号“1)”。