节点js在HTTP请求中接收文件 [英] Node js receive file in HTTP request

查看:211
本文介绍了节点js在HTTP请求中接收文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个服务器,该服务器可以从HTTP请求接收文件. 我使用Postman作为用户代理,并将文件添加到请求中.这是请求:

I try to create a server, which can receive a file from an HTTP request. I use Postman as user agent and I add a file to the request. This is the request:

POST /getfile HTTP/1.1
Host: localhost:3000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 9476dbcc-988d-c9bd-0f49-b5a3ceb95b85

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="test.xls"
Content-Type: application/vnd.ms-excel


------WebKitFormBoundary7MA4YWxkTrZu0gW--

但是当请求到达服务器时,我无法在其中找到文件(我的意思是在请求中). 我试图从请求的正文部分接收它,但是它返回> {}<.我试图弄清楚如何引用文件名,但不幸的是,我在请求标头中找不到文件名的引用...

But when the request reaches the server I can not find the file in it (I mean in the request). I tried to receive it from the body part of the request, but it returned > {} <. I tried to figure out, how can I refer to the name of the file, but unfortunately I can not find any reference in the request header for the name of the file...

有人可以帮助我找出答案,我该怎么办?

Can anybody help me to find out, what should I do?

推荐答案

作为我的评论的后续,您可以使用multer模块实现所需的操作: https://www.npmjs.com/package/multer

As a follow up to my comment, you can use the multer module achieve the thing that you want: https://www.npmjs.com/package/multer

const express = require('express');
const multer = require('multer');

const app = express();
const upload = multer();

app.post('/profile', upload.array(), function (req, res, next) {
  // req.body contains the text fields 
});

这篇关于节点js在HTTP请求中接收文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆