首页 > 解决方案 > 从 vscode RestClient 发送文件数组

问题描述

我在vsCode上使用 RestClient 来测试我的 API,并在后端使用Multer来保存文件。

我想知道有什么方法可以从 RestClient的 POST 请求中发送名为 productImages 的文件数组

标签: visual-studio-codemultipartform-data

解决方案


为了接受文件数组,您必须multer().array("productImages")在路由中使用中间件,然后multer将所有具有相同字段的表单数据添加name到文件数组中,您可以从req.files.

以下是上传 2 个文件的 restClient 发布请求:-


    post http://localhost:4225/add-product-images
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
    
    ------WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="productImages"; filename="1.png" 
    Content-Type: image/png
    
    < ../files/1.png
    
    ------WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="productImages"; filename="2.png" 
    Content-Type: image/png
    
    < ../files/2.png
    ------WebKitFormBoundary7MA4YWxkTrZu0gW--

推荐阅读