My Pdf file is stored in google bucket, and i have a link let say 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 .
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. It is not downloading in Chrome i guess Chrome do allow the downloading from CORS files.
There are tens of reasons which can cause this issue of downloading files instead of opening in browser. But mainly you get this issue due to poor hosting provider, any deflect in cache plugin you’re using on your website, or you messed up with the .htaccess file.
If your server is not updated then there are chances that it may be a reason for the downloading files instead of opening in browser error. To fix this issue, you’ll need to check whether your hosting provider has an update available or not. If there is any update available then you’ll need to upgrade/update it as soon as possible.
Then, the file automatically opens instead of saving when I download in Chrome To change this setting, start by clicking the "Tools" icon on your Chrome browser When you click on the three small lines dots (tools icon), you'll see a dropdown box appear. Click on "Settings" and you'll see a new page pop up in your Chrome browser window.
To download when you click instead of opening a link, just hold Alt when you left click. Voila! The file opens.
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.
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();
});
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')
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:
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:
self.set_header("Content-Disposition", 'filename=learned_data.json')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With