Ktoshni
asked on
Download Rather Than View
Hi,
I have a hyperlink in one of the pages of my web site which points to a text file. Currently when a user clicks on this hyperlink the browser will open a new window and display the contents of the text file. Rather than doing this I would like the browser to open a file download dialog box so that the user can download the file immediately. How can I do this?
Currently the code I am using is similar to:
<a href="file.txt"target=_bla nk>A File</a>
I have a hyperlink in one of the pages of my web site which points to a text file. Currently when a user clicks on this hyperlink the browser will open a new window and display the contents of the text file. Rather than doing this I would like the browser to open a file download dialog box so that the user can download the file immediately. How can I do this?
Currently the code I am using is similar to:
<a href="file.txt"target=_bla
It's actually about server configuration I believe, and if I am not mistaken it has something to do with the MIME types. I've never tried changing them so I'm not really sure what would be invovled.
let me look for more info
let me look for more info
I tried using type='application/octet-st ream' in the code where I specified a mime type that the browser would not open, didnt work for me =o(. Maybe I did it wrong or it jsut has to be done server side. Here's a PHP way of doing it
<?php
header('Content-Type: application/octet-stream') ;
?>
Good Luck,
Nick
<?php
header('Content-Type: application/octet-stream')
?>
Good Luck,
Nick
If you use Apache you can setup your .htaccess file to serve .txt files with a MIME that will force it to be downloaded. I would recommend application/octet-stream since it is the same used for .exe files and chances are people's computers wont open them without thier permission. This is the HTACCESS code to use....
AddType application/octet-stream .txt
do keep in mind all subdirectories with .txt files will be given this MIME so only put this HTACCESS file in a seperate directory with the file, so to not cause a headache when having to view plain .txt files.
AddType application/octet-stream .txt
do keep in mind all subdirectories with .txt files will be given this MIME so only put this HTACCESS file in a seperate directory with the file, so to not cause a headache when having to view plain .txt files.
ldbkutty thats the same answer I jsut gave =oP but using Apache instead of IIS, go check out the accepted answer. Take care! =o)
Regards,
Nick
Regards,
Nick
Actually, i didn't noticed your previous post CrYpTiC MauleR ... moreover, there are other suggestions in the link like using ZIP method is simpler..
Just thought Ktoshni might get some more information from the link.
:-)
Just thought Ktoshni might get some more information from the link.
:-)
here's some ASP that will work....
file.asp
========================== ========== =
<%
fileToDownload = Request.QueryString("toDow n")
getPathOnServer = Server.MapPath(fileToDownl oad)
Response.ContentType = "application/octet-stream" 'Suggest opening a download window
Response.AddHeader "content-disposition","att achment; filename=" & fileToDownload
Set adostream = Server.CreateObject("ADODB .Stream")
adostream.Open()
adostream.Type = 1
adostream.LoadFromFile(get PathOnServ er)
Response.BinaryWrite adostream.Read()
adostream.Close
%>
========================== ========== ==
on your page, link to the above file.asp like so:
<a href="/file.asp?toDown=doc ument.txt" >link</a>
hope that helps,
bruno
file.asp
==========================
<%
fileToDownload = Request.QueryString("toDow
getPathOnServer = Server.MapPath(fileToDownl
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","att
Set adostream = Server.CreateObject("ADODB
adostream.Open()
adostream.Type = 1
adostream.LoadFromFile(get
Response.BinaryWrite adostream.Read()
adostream.Close
%>
==========================
on your page, link to the above file.asp like so:
<a href="/file.asp?toDown=doc
hope that helps,
bruno
k just checking =o), but quick question, wouldnt a zip method need the client to have some zip program installed on thier comp? If not then using .blahness should also work. Never tested out a zip way so dont know if it requires the program on the client side. Lataz!
ASKER
Thanks for ur comments so far. We use JSPs on our server so can convert the page to a JSP if need be. The solutions you've given seem to be for ASP is there one for JSPs too?
On another note changing settings on the server is not an option. If possible it must be done through the page.
On another note changing settings on the server is not an option. If possible it must be done through the page.
CrYpTiC_MauleR (its hard to type ur login name :-) )
Even if the Client have ZIP program installed, it wont open directly ...it will display 'Download Dialog Box' only..
Compress programs like ZIP/RAR will function like that....You can try to download any ZIP/RAR files from any of the sites if you have installed the compress softwares.
:-)
Even if the Client have ZIP program installed, it wont open directly ...it will display 'Download Dialog Box' only..
Compress programs like ZIP/RAR will function like that....You can try to download any ZIP/RAR files from any of the sites if you have installed the compress softwares.
:-)
yeah but that would involve the user then having to change the file extension once they downloaded to view in text editor right?
<%@ page contentType="application/o ctet-strea m" %>
O.o
-
O.o
-
My suggestion was to put the text file in a zip file; not to compress it but to make it a file type that the browser won't try and display. Then after unzipping it will be in TEXT format.
but if the client does not have a zip program what do they do?
As stated in the link i posted,
> The zip is probably the easiest and best option.
>>>I can't be the only one who's run into this problem...
>You aren't and the solution is generally to use a zip or get into a more complex solution with scripting or messing around with the server.
If the client doesn't have any compress softwares, then ofcourse, its not possible to open.
> The zip is probably the easiest and best option.
>>>I can't be the only one who's run into this problem...
>You aren't and the solution is generally to use a zip or get into a more complex solution with scripting or messing around with the server.
If the client doesn't have any compress softwares, then ofcourse, its not possible to open.
But i assume that any user who has connected to internet would have Compression software program, as most available softwares in the world are available in Compressed formats only to be able to download.
Ktoshni,
I'm not familiar with JSP but it looks like Cryptic has the syntax you need here:
https://www.experts-exchange.com/questions/21001243/Download-Rather-Than-View.html#11152561
If you know JSP and can muddle your way through what my ASP is doing here :
https://www.experts-exchange.com/questions/21001243/Download-Rather-Than-View.html#11152410
Then you should be able to duplicate it...
I'm not familiar with JSP but it looks like Cryptic has the syntax you need here:
https://www.experts-exchange.com/questions/21001243/Download-Rather-Than-View.html#11152561
If you know JSP and can muddle your way through what my ASP is doing here :
https://www.experts-exchange.com/questions/21001243/Download-Rather-Than-View.html#11152410
Then you should be able to duplicate it...
Download a file instead of opening it. IE only and assuming you have ASP capabilities:
<%@Language="JScript"%>
<%
var myFSO = new ActiveXObject("Scripting.F ileSystemO bject")
var objFile = myFSO.openTextFile(Server. MapPath("/ path/somef ile.txt")) ;
var str = new String(objFile.readAll());
Response.ContentType = 'application/unknown';
Response.addHeader('conten t-disposit ion', 'attachment;filename=somef ile.txt');
Response.Write(str);
%>
Cd&
<%@Language="JScript"%>
<%
var myFSO = new ActiveXObject("Scripting.F
var objFile = myFSO.openTextFile(Server.
var str = new String(objFile.readAll());
Response.ContentType = 'application/unknown';
Response.addHeader('conten
Response.Write(str);
%>
Cd&
wow Cd&, server side scripting from you?? ;-)
I know a few bits and pieces because every once in a while I have to do stuff with Intel based toys running IIS. Fortuantely on the real servers we have mainframe strength generation tools that let us do whatever we want, in a sane and safe way.
Cd&
Cd&
yes yes, we know you are better than the rest of the world... :-)
ASKER
Sorry guys, tried the various things suggested but still no joy :(
must be a JSP thing then, because the ASP code posted here was tested and proven to work....sorry i don't know how to convert it.
https://www.experts-exchange.com/questions/21001243/Download-Rather-Than-View.html#11152410
https://www.experts-exchange.com/questions/21001243/Download-Rather-Than-View.html#11152410
In fact there are TWO problems:
- getting the server to send an octet-stream, so that the browser doens not display it directly -this is something that maybe controlled at the server side.
- when the browser gets an octet-stream, it asks what to do with it: open or download; the only way to escape this question to be asked is to define "local filetype" as being permanently donwloaded or opened; I would NOT recommend to permanently download txt files.
- getting the server to send an octet-stream, so that the browser doens not display it directly -this is something that maybe controlled at the server side.
- when the browser gets an octet-stream, it asks what to do with it: open or download; the only way to escape this question to be asked is to define "local filetype" as being permanently donwloaded or opened; I would NOT recommend to permanently download txt files.
The simplest way is:
1. Zipping it....(not compressing) Or,
2. Specifying 'Right Click and Save Target As'
Complex way (or innovative way) is to Code for it in JSP....(even most of us require one such thing)
Let us know what have you done to get the result.....
1. Zipping it....(not compressing) Or,
2. Specifying 'Right Click and Save Target As'
Complex way (or innovative way) is to Code for it in JSP....(even most of us require one such thing)
Let us know what have you done to get the result.....
Simplest:
<a href="textfile.txt"
onClick="alert('Please right-click and save');
return false">Text file</a>
Michel
<a href="textfile.txt"
onClick="alert('Please right-click and save');
return false">Text file</a>
Michel
PAQ it please. Usful reference but no definitive solution.
Cd&
Cd&
No points refunded here - question was abanded by the asker, not the experts.
bruno
bruno
Also good
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
One possible way is to ZIP the file(dont need to compress) so that the file type ZIP would not try to display but will ask to 'save as' option.
Or else you could give instructions to right click on the hyperlink select the "Save target as..." option.