简体   繁体   中英

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:

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.

How can I download this file directly using javascript (on the frontend, as my system is not running node)?

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; 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.

<a href=“Foo.xlsx” download>click to download</a> should do it for you.

Have a look at https://www.w3schools.com/howto/howto_html_download_link.asp for the browser support matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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