locked
File Download Handler. RRS feed

  • Question

  • User59157357 posted
    I have created a file download handler that is working as expected in all cases by one.

    I have a page where content is dynmaically generated by a repeater control. Inside each instance of the repeater control is an embedded button. Where I use the CommandName and CommandArgument properties of the button to redirect to the handler on the server. This all works fine. I can click on any of the buttons and the file will download like I should. My problem is that say I wanted to download a second file on the page. The commands no longer work untill I manually refresh the page.


    Is there something im doing wrong...

    Here is my code for calling the handler.


    If (e.CommandName = "DownloadPhoto") Then
                Dim Filename As String = e.CommandArgument
                Dim Path As String = "../ImageDownloadHandler.ashx?customer=Sprung&file=" & Filename
                Response.Redirect(Path)
    End If


    Here is the actual Handler code.

    Imports System
    Imports System.Web
    Imports System.IO

    Public Class ImageDownloadHandler : Implements IHttpHandler
       
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
           
            'Get the filename from the Query String...
            Dim strRequest As String = context.Request.QueryString("file")
           
            If strRequest <> "" Then
               
                Dim Path As String = context.Request.MapPath(context.Request.ApplicationPath & "/PhotoStorage/" & strRequest)
                Dim file As FileInfo = New FileInfo(Path) '-- if the file exists on the server
                Dim CustomerName As String = context.Request.QueryString("customer")
               
                Dim ChosenFile As String()
                Dim FileExtension As String
                Dim DownloadFilename As String
               
                If file.Exists Then 'set appropriate headers
                   
                    ChosenFile = file.Name.Split(".")
                    FileExtension = ChosenFile(1).ToLower
                    DownloadFilename = CustomerName & "." & FileExtension
                   
                    context.Response.Clear()
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" & DownloadFilename)
                    context.Response.AddHeader("Content-Length", file.Length.ToString())
                    context.Response.ContentType = "application/octet-stream"
                    context.Response.WriteFile(file.FullName)
                    context.Response.End() 'if file does not exist
                Else
                    context.Response.Write("This file does not exist. - ")
                End If 'nothing in the URL as HTTP GET
               
            Else
                context.Response.Write("Please provide a file to download.")
            End If
         
                  
        End Sub
     
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

    End Class



    Wednesday, May 24, 2006 12:43 PM

All replies

  • User59157357 posted
    Is there a hack for something like this... is there some code  I can add to my handler to redirect page to the page that made teh request.... or can I force a postpack on the main page... something?
    Wednesday, May 24, 2006 3:41 PM
  • User59157357 posted
    Do you think it would help if i opened a new window then redirected to the handler?
    Thursday, May 25, 2006 10:54 AM
  • User-158764254 posted

    rather than build a dynamically configured button that causes a postback/redirect,
    build a dynamically configured hyperlink with an href that points to your handler.

    also, i'd recommend replacing WriteFile with TransmitFile.
    WriteFile load the entire file into memory when it sends it.
    Transmitfile uses an internal buffer to prevent the entire file from being loaded into memory all at once.
    To use TransmitFile, you also need to set response.buffer to false so the buffers dont conflict. 
    I usually do that right aftter the response.clear.

    Friday, June 2, 2006 8:15 AM
  • User59157357 posted
    Thanks a lot. I changed my handler to transmit file instead of write file, and used the hyperlink instead of the button. And everything worked.

    Cheers

    James
    Friday, June 2, 2006 3:27 PM