Link to home
Start Free TrialLog in
Avatar of srazi
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 ..
ASKER CERTIFIED SOLUTION
Avatar of markhoy
markhoy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TTom
TTom

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
try this if you're worried about browser type:

Response.ContentType  = "application/x-msdownload"

for any type.
Hmmm.  That's an interesting suggestion.  I will try it with MY app.

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.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
Avatar of srazi

ASKER

Good stuff indeed markhoy..
Thanks & Regards
Just out of interest, if it worked, why a B grade?