Force download file - external URL - javascript

<a href="https://file-server/doc.txt" download> </a>
How can I force download file rather than open it in browser ?
I used download attribute, but it doesn't work with externals URLs
I had found a server-side solution but I don't have access to the file server
Any solutions please ?

Put files you wanna put to download on ZIP file. After you put to download, like:
<a download href = "[ZIP file with what you wanna put to download on]"></a>

Related

How can I download a file from firebase storage in react js?

I want to download a file from my firebase storage with the download url provided.
Instead the tag
"<a href={'file url'} download={'name'}>download"
opens the file in the browser instead of downloading it from firebase storage.
I have tried leaving the download attribute without a value! I expected it to download like it does for a file from the local source (the project directory). but the firebase download URL opens the file in the browser instead of downloading it from firebase storage on a device.
<a href={'file url'} download={'name'}>download</a>
Here in your code you're giving the link via the href attribute and then using the download attribute to make it a download link and then giving it a string.
The problem here is that the download attribute doesn't require any value but you gave it a string which is why it is not working.
To fix this, just make it:
<a href={'file url'} download>download</a>
Hope this helps :D

How do I write a function to autodownload pdf files from a link without navigating to another webpage either in java script or html

I am trying to implement the auto download feature in my web application where when clicked on a button a pdf file should download. I have the link but I am not able to download the file . I have used http get also and ionic file transfer plugin but neither of them are working. Can anyone help me write a code that does this task?
You can use the new download attribute for elements (new in HTML5). The following is from "https://www.w3schools.com/tags/att_a_download.asp"
"Specify a value for the download attribute, which will be the new filename of the downloaded file ("myFile.pdf" instead of "sampleFile.pdf"):"
Download PDF
Download PDF

How to download a text file onclick using javascript or HTML

I just want to download a .txt on click it. i am using the below html code:
Download
this code is working for .docx files not for .txt file.
You need to specify the file: protocol. Otherwise, the browser thinks that d: is a protocol name. You also need the download attribute to make it download the file instead of displaying it in the browser.
So it should be:
<a href="file:///d:/file.txt" download>Download</a>
But this seems pointless. The file d:/file.txt is already on your computer, why do you need to download it? Normally you download a file from the server to the client.
Text files are displayed in the browser when the content-type is sent as text. You should Try to send it with a different content-type or use a language such as PHP to send it as a download.
please try
Download
The download="my_text_file" attribute indicates that the target will be downloaded when clicked on the link.. and my_text_file will be new name for that file...
To expand on the answer from #barmar:
Taking the jquery question tag into consideration, check out Download File Using jQuery.
Here's the code example from that post:
$('a').click(function(e) {
e.preventDefault(); //stop the browser from following
window.location.href = 'uploads/file.doc';
});
Download now!
You can change the value assigned to window.location.href to the file you would like to download.
Try this:
<a href="d:/file.txt" download>Download</a>
It is working for me.
Cheers

Initiate file download in browser from JavaScript

I want to initiate a file download from react, such that the browser downloads it as any regular file.
When I use fetch, I can download the file and do what I want, but is not downloaded with the browsers download manager. The files are rather large, that is why I want the browser to manage the download.
I asked you if you have the full URL to the file.
If you want to start the download from a link, use the solution Kielstra provided. If you want to start the download using javascript, use the following code:
window.location = url_to_file;
If you have the URL to the file that you want to be downloaded, you can simply create a link to it with the download attribute.
<a href="link_to_file" download>Download</a>

Downloading File After Renaming Using HTML Or JavaScript

I want to download file from external server but after renaming it. Let's say http://playtictactoe.atwebpages.com/logo.png is an image file I want to download. I have used the following HTML:
<a href="http://playtictactoe.atwebpages.com/logo.png" download="myName.png">
Download File
</a>
But this doesn't rename the file. I've read somewhere that this is because of Response Header on server. Is there any way to ignore Response Header on client side? Else guide me any other solution.
You can download the file as a buffer and resave with the file api like descriped here:
HTML5 File API downloading file from server and saving it in sandbox
Or lookup FileAPI and XMLRequest to Buffer. You download the file as binaryBuffer save it with fileAPI and rename it. This should also work in Firefox. But this is not the simple solution you are searching for. Even though it works ;-)
You can then rename the file like you want.
Cheers

Categories

Resources