简体   繁体   English

在前端下载 Excel (.xlsx) 文件 Javascript 通过亚马逊预签名 URL

[英]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.

相关问题 使用预签名 URL 将文件从 javascript 前端上传到 S3 存储桶返回错误 400 - Uploading a file from javascript frontend to S3 bucket using presigned URL return error 400 如何使用 postdata preSigned Url 调用 Amazon S3 存储桶以使用空手道上传文件 - How to call Amazon S3 bucket using postdata preSigned Url to upload a file using Karate 如何使用预签名 url 在同一订单中为 Amazon S3 上传文件使用表单字段 - How to use form fields in the same order for Amazon S3 upload file using presigned url Sqlalchemy-创建 Amazon S3 预签名 URL - Sqlalchemy- Create Amazon S3 Presigned URL 使用预签名 URL 将文件放入 S3 - PUT file to S3 with presigned URL 无法在 s3 预签名 url 上上传文件 - Unable to upload file on s3 presigned url 通过 S3 的预签名 URL 限制上传文件的大小 - Limitting size of uploaded file via presigned URLs to S3 xlsx文件上传500错误。 亚马逊AWS - xlsx file uploading 500 error. Amazon AWS 为什么我需要通过预签名 url 上传到 s3 的存储桶策略? - Why do I need a bucket policy for uploading to s3 via presigned url? 无法使用节点预签名 url 从 React 将图像文件上传到 s3 - Unable to upload image file to s3 from React with Node presigned url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM