Link to home
Start Free TrialLog in
Avatar of VKSajeev
VKSajeev

asked on

Download option

How do I give a download facility for my site.As of now I have links to the documents to be downloaded and these just open up in the page.I need to give a download option to a user's harddisk.Please help me quickly
Avatar of Wakie
Wakie
Flag of Australia image

Something like this perhaps:

<a href="download.asp?file=anyfile.zip">Click here to download 'anyfile.zip'</a>

Then, in download.asp, have this:

<%
strFile = Request.QueryString("File")
Response.Redirect strFile
%>

Regards,
Wakie.
Avatar of VKSajeev
VKSajeev

ASKER

Sorry man
this is the error i get


Response object error 'ASP 0156 : 80004005'

Header Error

/common/groups/ushumanresources/AssociateBenefits/download.asp, line 7

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
Make sure you have your 'Response.Redirect' BEFORE you print any text or HTML.

If you are still stuck, post your code here :)

Regards,
Wakie.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%
strFile = Request.QueryString("File")
Response.Redirect strFile
%>
</head>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</html>

this is what i have in download.asp
Try this:

<%
strFile = Request.QueryString("File")
Response.Redirect strFile
%><html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</html>
DOWNLOAD LINK:

replace filename/path with yours!!

Response.Write " [" & _
               "<A HREF=""download.asp?FileName=" & _
               Server.URLEncode(objFile.Path) & _
               """ target=_blank>download code</A>]"
               "<br>"
               

DOWNLOAD.ASP

<%

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



%>
ps

<a href="download.asp?FileName=test.asp" target=_blank>TEST.ASP</a>

This code works, I use it on my site!
Thanks a lot man.
But this will this work only for a zip file?
I would like to give the actual word document for downloading.
try this code: this is for downloading a file without opening it in the browser, it will ask for open/save/cancel dialog box.


<%@Language="VBScript"%>
<%Option Explicit%>
<%Response.Buffer = True%>
<%
On Error Resume Next
Dim strPath
strPath = CStr(Request.QueryString("file"))
'-- do some basic error checking for the QueryString
If strPath = "" Then
  Response.Clear
  Response.Write("No file specified.")
  Response.End
ElseIf InStr(strPath, "..") > 0 Then
  Response.Clear
  Response.Write("Illegal folder location.")
  Response.End
ElseIf Len(strPath) > 1024 Then
  Response.Clear
  Response.Write("Folder path too long.")
  Response.End
Else
  Call DownloadFile(strPath)
End If
 
Private Sub DownloadFile(file)
  '--declare variables
  Dim strAbsFile
  Dim strFileExtension
  Dim objFSO
  Dim objFile
  Dim objStream
  '-- set absolute file location
  strAbsFile = Server.MapPath(file)
  '-- create FSO object to check if file exists and get properties
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  '-- check to see if the file exists
  If objFSO.FileExists(strAbsFile) Then
    Set objFile = objFSO.GetFile(strAbsFile)
      '-- first clear the response, and then set the appropriate headers
      Response.Clear
      '-- the filename you give it will be the one that is shown
      '   to the users by default when they save
      Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
      Response.AddHeader "Content-Length", objFile.Size
      Response.ContentType = "application/octet-stream"
      Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Open
        '-- set as binary
        objStream.Type = 1
        Response.CharSet = "UTF-8"
        '-- load into the stream the file
        objStream.LoadFromFile(strAbsFile)
        '-- send the stream in the response
        Response.BinaryWrite(objStream.Read)
        objStream.Close
      Set objStream = Nothing
    Set objFile = Nothing
  Else  'objFSO.FileExists(strAbsFile)
    Response.Clear
    Response.Write("No such file exists.")
  End If
  Set objFSO = Nothing
End Sub
%>


for further ref URL : http://www.xefteri.com/articles/may082002/default.aspx

I was replying to Wakie actually.Will your suggestion work for a .doc or .pdf?
Above code will work for all the files

:-SanjuBaba


???? WHat? the big select CASE statement reads the type of file and sets the download disposition accordingly. It works for ANY file type

This bit::

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


sets the content type according to the file type. ANY type!
Hi VKSajeev,

Nope, not only .zip files, but any file you like:

download.asp?file=anyfile.ext

Regards,
Wakie.
I tried that Wakie but the .doc file is opening in the html page itself.What to do?
ASKER CERTIFIED SOLUTION
Avatar of Wakie
Wakie
Flag of Australia 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
Any other way to make them not open in the browser?
Ur code looks the simplest and wud like to use that.any way out?
VKSajeev,

Did u try the code which i posted above, use that it is possible to download any type files. it will not open in the browser instead it will ask for download to ur system.

Thanks
The only way you can go about it with my script is to compress your Word document, into a ZIP or self-extracting executable.
Not true!  Simply change the mime type short-term!

Markov, your code works like a charm... go to https://www.experts-exchange.com/questions/20507444/Another-Download-Question-Project.html  I willgive you my points since I was looking for this exact code!
VKSajeev,

What is the status of this question?
VKSajeev,

This question is in danger of becoming classified as "abandoned".

Please take one of the appropriate actions in a timely manner:

a) Accept an expert's comment as an answer.
b) Provide more information into your problem.
c) If you have solved the problem, please post the solution and request to have this question closed at Community Support (https://www.experts-exchange.com/Community_Support/).

Wakie,
EE Cleanup Volunteer.
It appears this question has been abandoned.

I will leave a recommendation in the Cleanup topic area that this question will be:

- Question PAQ'd and points to Wakie -

Please leave any comments here within the next seven days.

DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Wakie,
EE Cleanup Volunteer.
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
The question states DOWNLOAD. Wakies code merely opens it in the browser. Suggesting yourself to receive points is surely "Unethical"?
Comment from VKSajeev  02/02/2003 07:56PM PST  

I tried that Wakie but the .doc file is opening in the html page itself.What to do?  

Wakie solution was NOT accepted by VKSajeev
The moderators here cannot be experts in all areas much as you cannot.  This is why we depend on the recommendation AND on the 7 day period for experts to object.  

Since an objection has been raised, I need input from as many experts here as I can get.  You indicate that the questioner challenged the above information, but yet I see Wakie addressed that response and continued to address all concerns raised by the questioner until such a time as the questioner abandoned the question despite several attempts to get them to come back.  I will allow 72 hours for feedback at which point I will revisit this question and make a final determination based on all the responses (including those already above).

I personally have had the "unethical" question several times.  If you'd like me to go into it again, I guess one more won't matter.

SpideyMod
Community Support Moderator @Experts Exchange
Revisited and no further responses.  No further review is scheduled.  Actions stand.

SpideyMod
Community Support Moderator @Experts Exchange