如何在Node.js中接收Multipart数据

时间:2018-07-27 05:25:00

标签: node.js

我正在使用Retrofit2 lib将数据从android应用发送到我的nodejs Web服务器 我正在发送带有一张图像的现场数据

Android代码

@Multipart
    @POST("u_post_i")
    Call<RxtraPostApi> post_with_img(
            @Part("Pid") RequestBody Pid,
            @Part("supportPid") RequestBody supportPid,
            @Part("msg") RequestBody msg,
            @Part MultipartBody.Part filePart);

这是我的nodejs代码

export function user_img_rec(req,res): Promise<hs_coman_return_obj> {
    try {
        console.log('A1: ', req.name.msg); // Getting error (TypeError: Cannot read property 'msg' of undefined)
       console.log('A3: ', req.body.msg); // Getting error (TypeError: Cannot read property 'msg' of undefined)
        console.log(req.body.toString('utf8')); //Getting With this kind of forment

**

--43441257-300c-4bd4-b4ca-af1b1cafb6c7
Content-Disposition: form-data; name="msg"
Content-Transfer-Encoding: binary
Content-Type: text/plain; charset=utf-8
Content-Length: 5

Hhhhh**
    } catch (e) {
        return Promise.reject(null);
    }
} 

有人知道如何在nodejs中读取属性和图像

1 个答案:

答案 0 :(得分:0)

您可以为此使用Multer-> https://www.npmjs.com/package/multer

Multer接受req和req,并在req对象中添加file(或files)属性。 这样您就可以读取req.file(req.files)中的文件。

Multer首先上传文件,然后设置一个或多个文件属性。因此,以防万一,您不想存储文件,而不必从文件系统取消链接。