srazi
asked on
How I prompt for Downloading a text file.
On one of the pages of my site I've to show a text file, which when clicked should prompt the user to Save or Open, common thing with all other downloadable files, rather It's opening the text file in the browser window.How do I achieve to ask it for the user to save rather than to open in the browser itself when clicked.
Thanks ..
Thanks ..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this if you're worried about browser type:
Response.ContentType = "application/x-msdownload"
for any type.
Response.ContentType = "application/x-msdownload"
for any type.
Hmmm. That's an interesting suggestion. I will try it with MY app.
Thanks!
Tom
Thanks!
Tom
if you look at the ee link I gave there is a big select case for lots of different file types. I use this to download files from my web server via browser (for when a firewall stops my ftp client):
<%
filename=request.querystri ng("filena me")
dim objFSO, objTS
Set objFSO = Server.CreateObject("Scrip ting.FileS ystemObjec t")
Set objTS = objFSO.OpenTextFile(fileNa me)
sFileType= Right(fileName,4)
Select Case sFileType
Case ".asf"
sContentType = "video/x-ms-asf"
Case ".avi"
sContentType = "video/avi"
Case ".doc"
sContentType = "application/msword"
Case ".zip"
sContentType = "application/zip"
Case ".css"
sContentType = "text/css"
Case ".pdf"
sContentType = "application/pdf"
Case ".xls"
sContentType = "application/vnd.ms-excel"
Case ".gif"
sContentType = "image/gif"
Case ".jpg", "jpeg"
sContentType = "image/jpeg"
Case ".wav"
sContentType = "audio/wav"
Case ".mp3"
sContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
sContentType = "video/mpeg"
Case ".rtf"
sContentType = "application/rtf"
Case ".htm", "html"
sContentType = "text/html"
Case ".asp", ".asa"
sContentType = "text/asp"
Case Else
sContentType = "application/x-msdownload"
End Select
NameFile=Right(FileName,Le n(FileName )-InstrRev (FileName, "\"))
Response.ContentType = sContentType
response.AddHeader "Content-Disposition", "attachment;filename=" & NameFile
Response.Buffer = True
Do While Not objTS.AtEndOfStream
strChunk = objTS.Read(32)
strTmp = ""
For i = 1 to Len(strChunk)
strTmp = strTmp & ChrB(Asc(Mid(strChunk, i, 1)))
Next
Response.BinaryWrite strTmp
Response.Flush
Loop
objTS.Close
Set objTS = Nothing
Set objFSO = Nothing
%><html>
<body onLoad="window.close()">
</body>
</html>
not tested with other browsers or computers other than PC and IE 5.5
<%
filename=request.querystri
dim objFSO, objTS
Set objFSO = Server.CreateObject("Scrip
Set objTS = objFSO.OpenTextFile(fileNa
sFileType= Right(fileName,4)
Select Case sFileType
Case ".asf"
sContentType = "video/x-ms-asf"
Case ".avi"
sContentType = "video/avi"
Case ".doc"
sContentType = "application/msword"
Case ".zip"
sContentType = "application/zip"
Case ".css"
sContentType = "text/css"
Case ".pdf"
sContentType = "application/pdf"
Case ".xls"
sContentType = "application/vnd.ms-excel"
Case ".gif"
sContentType = "image/gif"
Case ".jpg", "jpeg"
sContentType = "image/jpeg"
Case ".wav"
sContentType = "audio/wav"
Case ".mp3"
sContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
sContentType = "video/mpeg"
Case ".rtf"
sContentType = "application/rtf"
Case ".htm", "html"
sContentType = "text/html"
Case ".asp", ".asa"
sContentType = "text/asp"
Case Else
sContentType = "application/x-msdownload"
End Select
NameFile=Right(FileName,Le
Response.ContentType = sContentType
response.AddHeader "Content-Disposition", "attachment;filename=" & NameFile
Response.Buffer = True
Do While Not objTS.AtEndOfStream
strChunk = objTS.Read(32)
strTmp = ""
For i = 1 to Len(strChunk)
strTmp = strTmp & ChrB(Asc(Mid(strChunk, i, 1)))
Next
Response.BinaryWrite strTmp
Response.Flush
Loop
objTS.Close
Set objTS = Nothing
Set objFSO = Nothing
%><html>
<body onLoad="window.close()">
</body>
</html>
not tested with other browsers or computers other than PC and IE 5.5
ASKER
Good stuff indeed markhoy..
Thanks & Regards
Thanks & Regards
Just out of interest, if it worked, why a B grade?
One option is to rename your text files with an "unknown" extension, e.g., xyz. If there is no mime mapping for the extension, the user will be prompted to download the file. That always seems to be the case with .exe or .zip files.
Tom