【问题标题】:Angular Post Binary DataAngular Post 二进制数据
【发布时间】:2018-10-15 08:28:59
【问题描述】:

我正在尝试使用以下代码发布二进制数据:

let headers = new Headers()
headers.append('Content-Type', 'application/json')

this.http.post("http://localhost:8080/api/load",
    requestBytes,
    {
      headers: headers
    }).subscribe((data) => {
        console.log("success!!!")
    })

requestBytes 的类型为 Uint8Array

请求负载如下所示

{
   "0": 10,
   "1": 1,
   "2": 49
}

正如您所看到的,这似乎是一个 dict,而我预计它是一个字节数组。这会导致服务器混乱。

我尝试将 Content-Type 更改为 application/octet-stream 并传入 buffer 而不是数组,但这也没有帮助。在这种情况下,有效载荷是空的。

【问题讨论】:

  • 使用application/octet-stream 并发送requestBytes.buffer 而不是requestBytes
  • @AnshumanJaiswal 尝试过,正如我的问题的最后一个块中提到的那样

标签: javascript angular


【解决方案1】:

必须使用 XMLHttpRequest 手动创建请求。

最终代码如下所示:

const httpRequest = new XMLHttpRequest()
httpRequest.open('POST', 'http://localhost:8080/api/restaurant/load', true)
httpRequest.responseType = "arraybuffer"
httpRequest.send(requestBytes)

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2014-03-24
    • 2011-06-30
    • 2018-12-28
    相关资源
    最近更新 更多