?
Solved

Downloading files in ASP

Posted on 2003-03-17
8
Medium Priority
?
293 Views
Last Modified: 2010-04-06
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
Comment
Question by:LDTK
[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
8 Comments
 
LVL 4

Accepted Solution

by:
arthuryeung earned 400 total points
ID: 8151131
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
 
LVL 7

Assisted Solution

by:markhoy
markhoy earned 400 total points
ID: 8153695
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
 
LVL 15

Expert Comment

by:gladxml
ID: 8157589
LDTK,

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

http://www.aspemporium.com/aspEmporium/tutorials/vbs_download.asp
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 4

Expert Comment

by:whammy
ID: 8162861
Another solution that worked for me with very little modification, the first try!:

http://www.asp101.com/articles/jason/filedownloadlist/default.asp
0
 
LVL 4

Expert Comment

by:Wakie
ID: 8171273
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 9115252
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
 
LVL 6

Expert Comment

by:Programming_Gal
ID: 9712148
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

770 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