Angular http.post生成的csv文件

时间:2019-03-07 09:20:42

标签: angular csv http-post

问题 生成一个csvFile并将其发布到formData

将Postman与Body / form-data一起使用并选择一个csvFile可以正常工作, 服务器在浏览器中返回500(在这种情况下,有效载荷中找不到文件)

请求有效载荷

    ------WebKitFormBoundarynooBX9onHwqVvkji
 Content-Disposition: form-data; name="file"; filename="test.csv"
 Content-Type: text/csv;charset=utf-8;

 CSV FileContent...

可能是我想要的:Postman的有效载荷

content-type: multipart/form-data; boundary=--------------------------183666522156940808101506
    content-length: 2298
    file=[object Object]

角度代码

    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'multipart/form-data',
        'Authorization': authorization
      })
    };
    let formData = new FormData();
    let csvContent = this.fileContent.map(e => e.join(";")).join("\n");        
    let blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;"});
    formData.append('file', blob, "test.csv");            
    this.http.post(URL, formData , httpOptions).subscribe(
      data => console.log('success', data),
      error => console.log('error', error)
    );;

0 个答案:

没有答案