Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force fully download txt file on link?

I have one simple text file and I want to download that file on any anchor tag link.

But when I click on that link txt file shown me but not downloaded.

I have try this code

<html>     <head>         <title>File</title>     </head>     <body>         <a href="test.txt">Click here</a>     </body> </html> 
like image 500
Dhaval Avatar asked Jan 13 '14 09:01

Dhaval


People also ask

How do I force a file to download from a website?

In most browsers, clicking on the link will open the file directly in the browser. But, if you add the download attribute to the link, it will tell the browser to download the file instead. The download attribute works in all modern browsers, including MS Edge, but not Internet Explorer.


1 Answers

Download file when clicking on the link (instead of navigating to the file):

<a href="test.txt" download>Click here</a> 

Download file and rename it to mytextdocument.txt:

<a href="test.txt" download="mytextdocument">Click here</a> 

The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.

This attribute is only used if the href attribute is set.

The value of the attribute will be the name of the downloaded file. There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.).

If the value is omitted, the original filename is used.

like image 90
Efekan Avatar answered Sep 18 '22 09:09

Efekan