HTTP PUT 一个带有 Fetch API 的 PNG

HTTP PUT a PNG with Fetch API

我有一个名为 imgBase64 的 Base64 编码图像。

如何使用 Fetch API 从浏览器 HTTP PUT 此 PNG?

const response = await fetch(url, {
    method: 'put',
    headers: {
        'Content-Type': 'image/png'
    },
    body: atob(imgBase64) // <--------- what should I be doing here?
});

如果 imgBase64 已经编码,您可以添加 Content-Transfer-Encoding header 并删除 atob 方法:

const response = await fetch(url, {
    method: 'put',
    headers: {
        'Content-Type': 'image/png',
        'Content-Transfer-Encoding': 'base64'
    },
    body: imgBase64
});

这就是最终对我有用的东西:

let imageResponse = await fetch(imgBase64); 

const putResponse = await fetch(url, {
    method: 'put',
    headers: {
        'Content-Type': 'image/png',
    },
    body: await imageResponse.blob()
});