javascript下载图片需要referer

时间:2017-03-29 18:19:58

标签: javascript python imagedownload

当我尝试使用javascript下载时,我想下载的图片返回403

但我可以用下面的python代码下载图像。它将引用添加到标题并下载它。

req = urllib.request.Request(image_url, headers={"Referer": referer_url})
with urllib.request.urlopen(req) as response, open("image.jpg", "wb") as outfile:
    shutil.copyfileobj(response, outfile)

javascript可以下载这样的图像吗?

我正在尝试的Javascript代码。它的react-native-fetch-blob libaray

export const saveToonImageToLocal = (url, imageName) => {
  return RNFetchBlob
    .config({
      path: dirs.DocumentDir + `${imageName}.jpg`,
      appendExt: 'jpg'
    })
    .fetch('GET', url, {})
    .then((res)=>{
      return res.path();
    })
    .catch((e)=>{
      console.log('error occurend during saving images');
    })
};

1 个答案:

答案 0 :(得分:0)

您可以使用javascript ajax调用加载图片并在其中传递标题:

  $.ajax({
     type:'GET',
     url:'https://example.com/someimage.png',
     headers: {'Referer': "http://somepage.com"},
     success:function(data){
        $('#someImage').attr('src',data);
     }
  });