如何在输入类型文件中选择的节点js中获取文件路径(How to get file path in node js selected in the input type file)

编程入门 行业动态 更新时间:2024-10-09 07:26:21
如何在输入类型文件中选择的节点js中获取文件路径(How to get file path in node js selected in the input type file)

我是节点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 : undefined

I 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.

更多推荐

本文发布于:2023-08-07 17:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   节点   路径   类型   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!