简体   繁体   English

文件不是下载,而是在浏览器中打开

[英]Instead of downloading, files are opening in browser

My Pdf file is stored in google bucket, and i have a link let say https://storage.googleapis.com/bucketName/xyz.pdf .我的 Pdf 文件存储在谷歌存储桶中,我有一个链接,比如https://storage.googleapis.com/bucketName/xyz.pdf To download this file i am doing this,要下载这个文件,我正在这样做,

<a href="https://storage.googleapis.com/bucketName/xyz.pdf" download> Download This File </a>

But when i click on this anchor tag, instead of downloading this file browser open this file in same tab even i try to download the file via javascript and was using this code .但是当我点击这个锚标签时,浏览器没有下载这个文件,而是在同一个选项卡中打开这个文件,即使我尝试通过 javascript 下载文件并使用了这个代码。

var link = document.createElement("a");
link.download = 'File.pdf';
link.href = 'https://storage.googleapis.com/bucketName/xyz.pdf';
link.click();

But same happen again file open in same tab instead of downloading.但是同样的情况再次发生在同一个选项卡中打开文件而不是下载。 I don't know what is the main problem ?我不知道主要问题是什么? Is this Google bucket is not letting file to download, or my chrome setting preventing files to download.这个 Google 存储桶是不允许文件下载,还是我的 chrome 设置阻止文件下载。 It is not downloading in Chrome i guess Chrome do allow the downloading from CORS files.它不是在 Chrome 中下载,我猜 Chrome 确实允许从 CORS 文件下载。

As per JavaScript/jQuery to download file via POST with JSON data construct a blob and use that to return the file reference for the link. 根据JavaScript / jQuery通过POST使用JSON数据下载文件构造一个blob并使用它来返回链接的文件引用。

This will inform the browser of your intent in a standards compliance manner. 这将以符合标准的方式告知浏览器您的意图。

example ... 例子......

$.get(/*...*/,function (result)
{
    var blob=new Blob([result]);
    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="myFileName.txt";
    link.click();

});

Try link.target = "_blank"; 试试link.target = "_blank"; this will open file in new tab and link.download will force it download. 这将在新选项卡中打开文件, link.download将强制下载。

Please tell if this works. 请告诉我这是否有效。

Solution解决方案

Content-Disposition attachment seems to work for me:内容处置附件似乎对我有用:

self.set_header("Content-Type", "application/json")
self.set_header("Content-Disposition", 'attachment; filename=learned_data.json')

Workaround解决方法

application/octet-stream应用程序/八位字节流

I had something similar happening to me with a JSON, for me on the server side I was setting the header to self.set_header("Content-Type", " application/json ") however when i changed it to:我用 JSON 发生了类似的事情,对我来说,在服务器端我将标头设置为 self.set_header("Content-Type", " application/json ") 但是当我将其更改为:

self.set_header("Content-Type", "application/octet-stream")

It automatically downloaded it.它自动下载了它。

Also know that in order for the file to still keep the .json suffix you will need to it on filename header:还要知道,为了使文件仍然保留 .json 后缀,您需要在文件名标题上使用它:

self.set_header("Content-Disposition", 'filename=learned_data.json')

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

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