Monday, June 9, 2014

ASP.NET Generic Handler Not Downloading .pptx and .pdf files properly

There was a question raised in the forums as to why the generic handler doesn't download properly .ppt/.pdf files using Firefox but works perfectly using chrome and IE. The solution is to add a content-disposition header and set the ContentType property of context.Response.
VB.NET Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
 
        Dim documentID As Int32
        If Not context.Request.QueryString("documentID") Is Nothing Then
            documentID = Convert.ToInt32(context.Request.QueryString("documentID"))
        Else
            Throw New ArgumentException("No parameter specified")
        End If
  
        Dim strm As Stream = ShowDocument(documentID)
        context.Response.AddHeader("content-disposition", "attachment; filename=" & fileName.Replace(" ", "_") & ".pptx")
        context.Response.ContentType = docType
  
        If ShowDocument(documentID) Is Nothing Then
        Else
            strm = ShowDocument(documentID)
            Dim buffer As Byte() = New Byte(4095) {}
            Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)
 
            Do While byteSeq > 0
                context.Response.OutputStream.Write(buffer, 0, byteSeq)
                byteSeq = strm.Read(buffer, 0, 4096)
            Loop
        End If
 
    End Sub

0 comments:

Post a Comment