简体   繁体   English

FormData 中的 Nodejs 额外数据

[英]Nodejs Extra Data in FormData

I am sending a http request with a file to Sharepoint.我正在向 Sharepoint 发送带有文件的 http 请求。 Some file types, such as png or other images become corrupted and unreadable.某些文件类型(例如 png 或其他图像)已损坏且无法读取。 When looking at those I see extra object data at the head eg在查看那些时,我在头部看到额外的 object 数据,例如

----------------------------826320949470377643449533
Content-Disposition: form-data; name="file"; filename="mypdf.pdf"
Content-Type: application/pdf

and at the bottom:在底部:

----------------------------826320949470377643449533--

Is there a way to prevent this?有没有办法防止这种情况? The code I am using looks like:我正在使用的代码如下所示:

const contentType = mime.contentType(fileName);
const data = new FormData();
data.append('file',fs.createReadStream(path));
const fileSize = req.headers['content-length']
fs.promises.file(file)).size
const fileSizeLessByte = fileSize-1;
const contentRange = 'bytes 0-'+fileSizeLessByte+'/'+fileSize;
const contentLength = fileSize;

var config = {
    method: 'put',
    url: uploadUrl,
    headers: { 
      'Content-Range': contentRange,
      'Content-Length': contentLength,
      'Content-Type': 'multipart/form-data'
    },
    data : data
  };

I was eventually able to get this working by using readFileSync instead of FormData and not using the multipart content type as O. Jones suggested:我最终能够通过使用 readFileSync 而不是 FormData 来完成这项工作,而不是像 O. Jones 建议的那样使用多部分内容类型:

 const contentType = mime.contentType(fileName);
 const fileStream = fs.readFileSync(path);
 const fileSize = Buffer.byteLength(fileStream);
 const fileSizeLessByte = fileSize-1
 const contentRange = 'bytes 0-'+fileSizeLessByte+'/'+fileSize;
 const contentLength = fileSize;

 const config = {
     method: 'put',
     url: uploadUrl,
     headers: { 
         'Content-Type': contentType,
         'Content-Range': contentRange,
         'Content-Length': contentLength
     },
     data: fileStream
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM