?
Solved

How I prompt for Downloading a text file.

Posted on 2003-03-09
7
Medium Priority
?
644 Views
Last Modified: 2011-09-20
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 ..
0
Comment
Question by:srazi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
markhoy earned 150 total points
ID: 8097846
Here is an article that should help you clear up this question;
http://www.aspemporium.com/aspEmporium/tutorials/vbs_download.asp

Set the a href on the text file to point to this asp page (code as below, substitute names as appropriate)

here is a script that uses the ADO Stream method;

<%
DownloadFileName = Request.QueryString("FileName")
DownloadFolder = "d:\test"
BaseFileName = Split(DownloadFileName, ".")

Set ObjFile = Server.CreateObject("Scripting.FileSystemObject")
FilePath = Server.MapPath(DownloadFolder)

'Define path to file named in ASP format
InitFileName = FilePath & "\" & BaseFileName(0) & ".asp"

'Define path for temporary download file
DownloadFileName = FilePath & "\" & Replace(Request.ServerVariables("REMOTE_ADDR"),".","") & ".txt"

'Copy file from .ASP to .PDF for downloading
If ObjFile.FileExists(InitFileName) then
ObjFile.CopyFile InitFileName, DownloadFileName
%>

<%
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open
'Read the file
objStream.LoadFromFile DownloadFileName
'Send the data to the browser
Response.Expires = 0
Response.Buffer = True
Response.ContentType = "application/text"
Response.Addheader "Content-Disposition", "inline; filename=" & "stream.txt"
Response.Addheader "Content-Length", objStream.size
Response.BinaryWrite(objStream.Read(adReadAll))

'Delete temporary download file and close objects
ObjFile.DeleteFile DownloadFileName
objStream.Close
Set objStream = Nothing
Set objFile = Nothing

Else
Response.Write "File not found...."
End if
Response.End()
%>

see here for more examples.
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20475067.html#1
0
 
LVL 9

Expert Comment

by:TTom
ID: 8097988
FWIW, I think the content disposition header only works with IE.  When I used a stream object, I needed to do something else for NS (but I don't have the code handy).

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
0
 
LVL 7

Expert Comment

by:markhoy
ID: 8098059
try this if you're worried about browser type:

Response.ContentType  = "application/x-msdownload"

for any type.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:TTom
ID: 8098095
Hmmm.  That's an interesting suggestion.  I will try it with MY app.

Thanks!

Tom
0
 
LVL 7

Expert Comment

by:markhoy
ID: 8098106
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.querystring("filename")


dim objFSO, objTS
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(fileName)

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,Len(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
0
 

Author Comment

by:srazi
ID: 8101290
Good stuff indeed markhoy..
Thanks & Regards
0
 
LVL 7

Expert Comment

by:markhoy
ID: 8101777
Just out of interest, if it worked, why a B grade?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question