如何使用二進制體節點js上傳文件

[英]How to upload file with binary body node js


Not able to upload file.Is this right way for binary body upload

無法上傳文件。這是二進制正文上傳的正確方法

fs.readFile("Koala.jpg", function(err, data) {
  if (err) throw err;

 request( {
      url     : url,
      method  : POST,
      json    : true,
      body    : data, 
      encoding: null,
      headers : {
    "Content-Type": "application/octet-stream"
      }
});
});

tried changing header content type to image/jpeg tried with removing encoding null. works fine with postman.

嘗試將標題內容類型更改為image / jpeg嘗試刪除編碼null。與郵遞員一起工作。

https://imgur.com/a/wS0tS

https://imgur.com/a/wS0tS

1 个解决方案

#1


0  

Easiest way that I've found to upload binary file or image is to use the form-data module along with fs as it eases the pain in submitting a multipart/form-data encoded request. The general steps if you decide to use this method are:

我發現上傳二進制文件或圖像的最簡單方法是使用form-data模塊和fs,因為它減輕了提交multipart / form-data編碼請求的痛苦。如果您決定使用此方法,一般步驟如下:

  1. Create a new form using form-data
  2. 使用表單數據創建新表單
  3. Use the form.append method along with the fs.createReadStream method to construct a form containing a file stream or Buffer
  4. 使用form.append方法和fs.createReadStream方法構造包含文件流或Buffer的表單
  5. Create your POST request
  6. 創建POST請求
  7. Use the form.pipe method to pipe your POST request
  8. 使用form.pipe方法來管道您的POST請求

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/10/04/7302760a82d84f603dc46ff749378625.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com