上传图片时出错 - imgur - 无效的网址

问题描述 投票:0回答:1

我有一些与imgur api有关的麻烦。我将图像转换为base64代码并尝试将其上传到imgur api。不幸的是我收到了一个错误:

"error": "Invalid URL (data:image/png;base64,iVBORw0KGgoAA..."

这是我的功能:

uploadImageToImgur: function (file) {
const url = 'https://api.imgur.com/3/image',
      reader  = new FileReader();

  reader.onloadend = async function () {
    let { result } = reader;

    try {
      const request = await fetch(url, {
        method: 'POST',
        headers: {
          "Authorization": 'my client key',
        },
        body: result
      });

      const response = await request.json();
      console.log(response);
    } catch (e) {
      throw new Error(e);
    }
  }

  if (file) {
    reader.readAsDataURL(file);
  }
}
image filereader imgur
1个回答
0
投票

你缺少一些参数。另外,请确保您的标头具有Client-ID密钥。

const request = await fetch(url, {
  method: 'POST',
  headers: {
    "Authorization": 'Client-ID {yourKey}',
  },
  form: {
    "image": result,
    "type": "base64"
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.