角度:从上传的api获取有效负载响应

时间:2019-04-02 09:31:22

标签: angular file-upload upload rxjs

使用此代码,我可以上传文件,但无法获得有效载荷响应,即新文件的base64表示形式。

bash $ SEARCH="SOMETEXT"
bash $ <file.json jtc -w"<$SEARCH>R:[-3]"
{
   "id": 729384872903,
   "notes": [
      {
         "body": "Some more more normal text [SOMETEXT]",
         "id": 983012
      },
      {
         "body": "Some more more more normal text",
         "id": 89023432
      }
   ]
}
bash $ 

我尝试了不同的this.http.post(config.url, formData, { reportProgress: true, responseType: 'arraybuffer' }).subscribre(res => { console.log(res); }); responseTypeblobarraybuffertext),但是在任何情况下我都无法获得有效载荷中的base64。

编辑1: 在Web控制台中,响应为bse64字符串

但是json返回:

  • subscribe,如果ArrayBuffer: {bytelength: 3717}responseType
  • arraybuffer,如果Blob: {size: 3717, type: 'application/octet-stream'}responseType
  • 如果blobresponseType的请求正文
  • 如果textresponseType则错误

1 个答案:

答案 0 :(得分:0)

我只需要将ArrayBuffer转换为base64字符串

const base64= btoa(String.fromCharCode.apply(null, new Uint8Array(res)));