【问题标题】:How to convert HTML input type="file" to a octet-stream如何将 HTML 输入类型 =“文件”转换为八位字节流
【发布时间】:2020-11-05 10:50:21
【问题描述】:

我正在制作一个 Web 应用程序,用户可以在其中使用来选择图像

<input id="file" type="file" accept="image/*" capture>

我需要将所选图像上传到 Azure,以便进一步处理它。 Azure 文档声明我必须使用 POST 调用,并将 Content-Type 设置为“application/octet-stream”,并且正文将包含图像文件。

我有以下几点:

const fileEl = document.getElementById('file');
fileEl.addEventListener( 'change', async (e) => {
  const file = e.target.files[0];

  const response = await fetch( "https://eastus.api.cognitive.microsoft.com/customvision/v3.0/Prediction/abc/detect/iterations/Iteration0/url", {
     method: "POST",
     headers: {
        "Prediction-Key": "123",
        "Content-Type": "application/octet-stream"
     },
     body: file
  } );

  console.log( response.ok );
} );

问题是这失败了……我猜 body: file 不正确。如何将文件对象转换为我应该在 POST 请求中发送的内容?

【问题讨论】:

    标签: ajax azure file post


    【解决方案1】:

    我在 fetch 命令中有错误的 url,这就是为什么没有任何工作!我修复了它和正文:文件工作得很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-27
      • 2020-04-09
      • 2022-08-20
      • 1970-01-01
      • 2020-01-10
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多