• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Downloading files in ASP

Hi I am trying to add a download facility to my web application. It was working ok until today but for some reason it is downloading the file name only instead of the file contents. i.e. when I open the file it only has the file name in it.I would really appreciate any help with this
Here is my code.

 Private Sub DownloadFile(ByVal fname As String)
        Dim path As System.IO.Path

        Dim forceDownload As Boolean

        forceDownload = True
        Dim fullpath = path.GetFullPath(fname)
        Dim name = path.GetFileName(fullpath)
        Dim ext = path.GetExtension(fullpath)
        Dim type As String = ""

        If Not IsDBNull(ext) Then
            ext = LCase(ext)
        End If

        Select Case ext
            Case ".htm", ".html"
                type = "text/HTML"
            Case ".txt"
                type = "text/plain"
            Case ".doc", ".rtf"
                type = "Application/msword"
            Case ".csv", ".xls"
                type = "Application/x-msexcel"
            Case Else
                type = "text/plain"
        End Select



        If (forceDownload) Then
            Response.Clear()
            Response.AddHeader("content-disposition", "attachment; filename=" & name)

        End If
        If type <> "" Then
            Response.ContentType = type
        End If

        Response.Write(fullpath)
        Response.End()

end sub

0
LDTK
Asked:
LDTK
2 Solutions
 
arthuryeungCommented:
you need to get the content of the file and response.binarywrite the file content to the client.
what you do it just writting file path in the file content

modify you code like this:

Private Sub DownloadFile(ByVal fname As String)
       Dim path As System.IO.Path

       Dim forceDownload As Boolean

       forceDownload = True
       Dim fullpath = path.GetFullPath(fname)
       Dim name = path.GetFileName(fullpath)
       Dim ext = path.GetExtension(fullpath)
       Dim type As String = ""

       If Not IsDBNull(ext) Then
           ext = LCase(ext)
       End If

       Select Case ext
           Case ".htm", ".html"
               type = "text/HTML"
           Case ".txt"
               type = "text/plain"
           Case ".doc", ".rtf"
               type = "Application/msword"
           Case ".csv", ".xls"
               type = "Application/x-msexcel"
           Case Else
               type = "text/plain"
       End Select



       If (forceDownload) Then
           Response.Clear()
           Response.AddHeader("content-disposition", "attachment; filename=" & name)

       End If
       If type <> "" Then
           Response.ContentType = type
       End If

       Set objStream = Server.CreateObject("ADODB.Stream")
       objStream.Open
       objStream.Type = adTypeBinary
       objStream.LoadFromFile fullpath
       Response.BinaryWrite objStream.Read
       Response.Flush

       objStream.Close
       Set objStream = Nothing

end sub
0
 
markhoyCommented:
Also see more examples here:

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20543964.html


I use this to download files (path and name passed to script):

<%

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



%>
0
 
gladxmlCommented:
LDTK,

you can also try to check out the link might help...

http://www.aspemporium.com/aspEmporium/tutorials/vbs_download.asp
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
whammyCommented:
Another solution that worked for me with very little modification, the first try!:

http://www.asp101.com/articles/jason/filedownloadlist/default.asp
0
 
WakieCommented:
Hi LDTK, take a look at the following link.

Download option
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20490839.html

Regards,
Wakie.
0
 
COBOLdinosaurCommented:
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

<note>
Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
</note>

If the user does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp


Cd&

0
 
Programming_GalCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between arthuryeung, markhoy

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Programming_Gal
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now