【发布时间】: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 请求中发送的内容?
【问题讨论】: