954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to use href to open text files.

Hi all,

I am giving the href to give a link to a file.
The problem is that in IE it opens the text files in the same browser, but in mozilla it works fine(gives prompt of save as option).


I want the same functionality for IE also, searching Google I found so much solution but all were saying about writing php.

I don't want to implement php, can it be done simply using by HTML,jsp and struts tag.

Regards,

cmaheshwari16
Newbie Poster
18 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I don't think you need php, but you will probably have to use some javascript or something if you want to force the link to be downloaded or saved.

You could use the target link attribute if you just need to stop it from opening in the same window as your current page.

Hearth
Posting Whiz in Training
224 posts since Apr 2008
Reputation Points: 106
Solved Threads: 33
 

I dont want to open it at all, what I want is the save as option should pop up. Like if some junk extension file you will give it will do.

File.asdf Link will give save as pop up
File1.doc or .txt will open the content of the file in the browser window.

cmaheshwari16
Newbie Poster
18 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Type the following magical incantation next to the link

"To save the file, right click the link and select Save as.."

Then the powers of darkeness and psychic forces extraordinary are invoked when the user does as he/she is told, and they can save the file.

drjohn
Posting Pro in Training
447 posts since Mar 2010
Reputation Points: 76
Solved Threads: 80
 

Great reply drjohn, thanks for the same. But I am looking for some concrete solution. How we can pop up a window when user clicks on the link.

cmaheshwari16
Newbie Poster
18 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Well, a quick check using the keyword 'force download', indicates you can force downloads, but it isn't easy. The easiest way is to simply zip the file you want to have downloaded, since zipped files are always downloaded.

Dandello
Posting Whiz in Training
263 posts since May 2010
Reputation Points: 28
Solved Threads: 23
 

one way
<a href="pathto/savefile.php?file=pathtosomefile.txt">download somefile.txt</a>
savefile.php is below

<?php $file=$_GET['file'];
if(!$file) return false;
header("Content-disposition: attachment; filename=$file");
header('Content-type: text/plain;');
readfile("$file");
?>
almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: