[英]Downloading Excel (.xlsx) File In Frontend Javascript Via Amazon Presigned URL
I have an amazon S3 URL being passed back to me from an external service that is in the format:我有一个 amazon S3 URL 从格式如下的外部服务传回给我:
https://foo.s3.amazonaws.com/ProjectDownloads/foo/title.xlsx?AWSAccessKeyId=foo&Expires=1602279238&Signature=foo https://foo.s3.amazonaws.com/ProjectDownloads/foo/title.xlsx?AWSAccessKeyId=foo&Expires=1602279238&Signature=foo
I haven't worked much with S3, but my understanding is this is a Presigned URL - I can paste it in my browser and the excel file downloads.我对 S3 的工作不多,但我的理解是这是一个预签名的 URL - 我可以将它粘贴到我的浏览器和 excel 文件下载中。
How can I download this file directly using javascript (on the frontend, as my system is not running node)?我怎样才能直接使用 javascript 下载这个文件(在前端,因为我的系统没有运行节点)?
I have tried generating an tag element with click - however all my files download as corrupt.我已尝试通过单击生成标记元素 - 但是我的所有文件都已损坏下载。 I can download the file directly by pasting the link in my browser and it auto initiates a download.
我可以通过在浏览器中粘贴链接直接下载文件,它会自动启动下载。
Any help will be appreciated.任何帮助将不胜感激。
I have tried the following, but it downloads a file with error:我尝试了以下方法,但它下载了一个错误的文件:
var link = document.createElement('a');
link.href = url;
link.download = 'foo.xlsx';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
You don't need JS to force a download on clicking the link;您不需要 JS 来强制单击链接进行下载; you can do this with just HTML. And, it doesn't matter that the source is a presigned S3 URL - it works with any URL.
你可以只用 HTML 来做到这一点。而且,源是预签名的 S3 URL 并不重要 - 它适用于任何 URL。
<a href=“Foo.xlsx” download>click to download</a>
should do it for you. <a href=“Foo.xlsx” download>click to download</a>
应该会为您完成。
Have a look at https://www.w3schools.com/howto/howto_html_download_link.asp for the browser support matrix.查看https://www.w3schools.com/howto/howto_html_download_link.asp浏览器支持矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.