簡體   English   中英

HTTP PUT a PNG with Fetch API

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM