简体   繁体   中英

HTTP PUT a PNG with Fetch API

I have a Base64 encoded image called imgBase64 .

How can I HTTP PUT this PNG from a browser using the Fetch API?

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:

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()
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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