1

I just want to download a .txt on click it. i am using the below html code:

<a href="d:/file.txt">Download</a>

this code is working for .docx files not for .txt file.

Phiter
  • 13,983
  • 14
  • 50
  • 82

5 Answers5

1

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.

Barmar
  • 669,327
  • 51
  • 454
  • 560
0

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.

0

please try

<a href="d:/file.txt" download="my_text_file">Download</a>

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...

0

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';
});

<a href="no-script.html">Download now!</a>

You can change the value assigned to window.location.href to the file you would like to download.

jhyry-gcpud
  • 115
  • 2
  • 7
-1

Try this:

<a href="d:/file.txt" download>Download</a>

It is working for me.

Cheers

JohnyJohnson
  • 137
  • 1
  • 10
  • Ohh, then why it is not working in my case. i am using google chrome. and same code copy pasted from here. – Phanindra Kumar Aug 29 '18 at 16:50
  • What happens when you press the link? It just straight up opens the file instead of opening the download promt? I tried it on Firefox, altho this solution is supposed to be working in all browsers. Have you tested on Firefox? – JohnyJohnson Aug 30 '18 at 10:17