首页 > 解决方案 > HTTP PUT 一个带有 Fetch API 的 PNG

问题描述

我有一个名为imgBase64.

如何使用 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?
});

标签: javascriptbase64fetch-api

解决方案


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

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

推荐阅读