Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How I prompt for Downloading a text file.

Posted on 2003-03-09
7
Medium Priority
?
649 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Screencast - Getting to Know the Pipeline
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

571 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