简体   繁体   English

从 s3 存储桶在 web 中强制下载移动应用程序上传的图像。 它在选项卡中打开而不是下载

[英]Download mobile app uploaded image forcefully in web from s3 bucket. It is opening in tab instead of downloading

I am trying to download an image on click and using "download" attribute of HTML5 for this.我正在尝试单击下载图像并为此使用 HTML5 的“下载”属性。 this image has been uploaded by mobile app in s3 bucket.此图像已由移动应用程序上传到 s3 存储桶中。 But it is redirecting the user to a new tab instead of downloading the image.I want to download this image directly.但它会将用户重定向到新选项卡,而不是下载图像。我想直接下载此图像。

<a href="<link>" download="file.jpg" target="_blank">Download image</a>

how can I solve this..?我该如何解决这个..? thanks in advance提前致谢

Try this code :试试这个代码:

function forceDownload(blob, filename) {
 var a = document.createElement('a');
  a.download = filename;
  a.href = blob;
  document.body.appendChild(a);
  a.click();
  a.remove();
}

// Current blob size limit is around 500MB for browsers
function downloadResource(url, filename) {
  if (!filename) filename = url.split('\\').pop().split('/').pop();
  fetch(url, {
      headers: new Headers({
        'Origin': location.origin
      }),
      mode: 'cors'
    })
    .then(response => response.blob())
    .then(blob => {
      let blobUrl = window.URL.createObjectURL(blob);
      forceDownload(blobUrl, filename);
    })
    .catch(e => console.error(e));
}

downloadResource('https://pixxort-prod.s3.amazonaws.com/records/c6e6d1617041434da83ab5b959278fc41604921530373.jpg');

It will force browser to download resource from web.它会强制浏览器从网络下载资源。

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

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