简体   繁体   English

有没有一种方法可以将Blob网址图片上传到imgur?

[英]Is there a way to upload blob url image to imgur?

I try to upload the image to imgur by api via javascript. 我尝试通过javascript通过api将图像上传到imgur。

I used the base64 code first, but I think the storage is too big. 我首先使用了base64代码,但我认为存储空间太大。

I changed the type of the file to blob. 我将文件的类型更改为blob。

but the post request status is 400. 但发布请求状态为400。

data:{
error: "Invalid URL ("blob:http//...")",
method: "POST",
request: "/3/image"
}

or 要么

data:{
    error: "Invalid URL (Array)",
    method: "POST",
    request: "/3/image"
    }

as following my code: 如下我的代码:

async dataCallBack(file){
    //file = "blob http..."
    var imageFile = new File([file], "giftpack.jpg");
    let response = await imgurPost(imageFile); //or file
    console.log(response);
    const result = response.data.data;
    const link = result.link;
    this.setState({url: link});
  }

imgurPost(file){
  return axios.post('https://api.imgur.com/3/image',
  {
    image: file,
    type: 'URL',
    album: '0gECV',
  }
  ,
  {
    headers:{
      'Authorization': 'Client-ID {{clientId}}',
      'Authorization': 'Bearer {{accessToken}}'
    }
  }).catch(e=>{
    console.log(e.response);
  })
}

Is there the way to use blob type? 有没有办法使用Blob类型?

A blob URL only works locally (and temporarily), so you can't send a blob with that. Blob URL仅在本地(且暂时)有效,因此您无法发送Blob URL。

You can use FormData . 您可以使用FormData

var fd = new FormData();
fd.append('image', file);
fd.append('type', 'URL');
fd.append('album', '0gECV');

(You can find examples of using FormData with your specific XHR helper elsewhere.) (您可以在其他地方找到将FormData与特定的XHR帮助程序一起使用的示例。)

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

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