我是节点js的新手。 我有和html表单有文件输入像 -
<form action="importlist_action" method="post" enctype="multipart/form-data"> Select File: <input type="file" name="file" id="file" tabindex="1" /> </form>现在,我已将此发布请求重定向到以下控制器方法。
exports.doImport = function(req, res) { console.log('Post values: ' + JSON.stringify(req.body)); console.log("File path : " + req.body.file); }我得到的结果是 -
Post values: {} File path : undefined我想要在控制器中上面的表单上选择的文件的路径。 有人可以纠正我在这里做错了吗?
任何帮助将非常感激。
I am new to the node js. I have and html form which is having file input like -
<form action="importlist_action" method="post" enctype="multipart/form-data"> Select File: <input type="file" name="file" id="file" tabindex="1" /> </form>Now, I have redirected this post request to the following method of controller.
exports.doImport = function(req, res) { console.log('Post values: ' + JSON.stringify(req.body)); console.log("File path : " + req.body.file); }The result I am getting here is -
Post values: {} File path : undefinedI want the path of the file which is selected on the form above in the controller. can some one please correct me what I am doing wrong here?
Any help will be really appreciated.
最满意答案
如果您正在使用Express 3.x和bodyParser()中间件,那么您应该在req.files下req.files文件。
如果您使用的是Express 4.x,则必须使用单独的模块来读取多部分请求。 这样的模块的例子是: busboy (/ connect- busboy / multer / reformed )和强大的 。
正如@adeneo所提到的,并非所有浏览器都提供实际的文件路径 ,但大多数浏览器至少会提供文件名。 Busboy和Formidable都可以使用文件名。
If you're using Express 3.x and the bodyParser() middleware, then you should look under req.files for files.
If you're using Express 4.x, then you have to use a separate module to read a multipart request. Examples of such modules are: busboy(/connect-busboy/multer/reformed) and formidable.
As @adeneo mentioned, not all browsers provide the actual file path, but most will at least supply a filename. Both Busboy and Formidable make the filename available to you.
更多推荐
发布评论