简体   繁体   English

HTTP PUT a PNG with Fetch API

[英]HTTP PUT a PNG with Fetch API

I have a Base64 encoded image called imgBase64 .我有一个名为imgBase64的 Base64 编码图像。

How can I HTTP PUT this PNG from a browser using the Fetch API?如何使用 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?
});

If the imgBase64 is already encoded, you can add Content-Transfer-Encoding header and remove atob method:如果imgBase64已经编码,则可以添加Content-Transfer-Encoding header 并删除atob方法:

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

This is what ended up working for me:这最终为我工作:

let imageResponse = await fetch(imgBase64); 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM