This is a migrated thread and some comments may be shown as answers.

Opening files from RadGrid

1 Answer 195 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josue
Top achievements
Rank 1
Josue asked on 21 Jan 2011, 10:51 PM
Hi Guys,

Hope you can help me with this.

I got a Grid with a GridTemplateColumn and other informational columns, and inside the GridTemplateColumn is a ImageButton that will execute a function that will open documents. Here is the code:
Public Sub OpenDocument(ByVal sAttachmentPath As String)
    Dim sAttachmentExt As String
    Response.Clear()
    Response.ClearHeaders()
    Response.ClearContent()
    sAttachmentExt = sAttachmentPath.Substring(sAttachmentPath.LastIndexOf(CChar(".")))
    Try
        Select Case sAttachmentExt
            Case ".doc"
                Response.ContentType = "application/msword"
            Case ".docx"
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            Case ".xls"
                Response.ContentType = "application/vnd.ms-excel"
            Case ".xlsx"
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            Case ".ppt"
                Response.ContentType = "application/vnd.ms-powerpoint"
            Case ".pptx"
                Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
            Case ".pdf"
                Response.ContentType = "application/pdf"
            Case ".html"
                Response.ContentType = "application/html"
            Case ".txt"
                Response.ContentType = "application/plain"
            Case ".zip"
                Response.ContentType = "application/zip"
            Case ".rar"
                Response.ContentType = "application/rar"
            Case ".gif"
                Response.ContentType = "application/gif"
            Case ".jpg"
                Response.ContentType = "application/jpeg"
            Case ".png"
                Response.ContentType = "application/png"
            Case ".swf"
                Response.ContentType = "application/x-shockwave-flash"
            Case ".mpeg"
                Response.ContentType = "application/mpeg"
            Case ".avi"
                Response.ContentType = "application/x-msvideo"
            Case Else
                Response.ContentType = "application/plain"
        End Select
 
        Response.AddHeader("Content-Disposition", "attachment;filename=" & sAttachmentPath)
        Dim sourcefile As FileStream = New FileStream(sAttachmentPath, FileMode.Open)
 
        Dim FileSize As Integer
        FileSize = sourcefile.Length
        Dim getContent() As Byte = New Byte(FileSize - 1) {}
 
        sourcefile.Read(getContent, 0, CInt(sourcefile.Length))
        sourcefile.Close()
        Response.BinaryWrite(getContent)
        Response.Flush()
 
    Catch ex As Exception
        Throw ex
    Finally
        Response.End()
    End Try
End Sub

All work fine for the first time. But as soon I do a sorting of columns or filtering or change page, the Response.End() gives me an error of "Unable to evaluate expression". As a postback is made it start working again. Also I used HttpContext.Current.ApplicationInstance.CompleteRequest instead of Response.End() to see if continue working, in this case the error is not given, but still doesn't open the document. I know this is not a error of yours, but maybe there's something I could set to the grid to let me do what i need. Please let me know if you need any other information.

Thanks,
Josue

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 27 Jan 2011, 06:54 PM
Hello Josue,

Cannot see anything that RadGrid may be interfering with. Try a simplified process of writing the attachment to the response:

Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("content-disposition", "attachment; filename=" + sAttachmentPath)
Response.BinaryWrite(getContent)
Response.End()

This should stream any file type. Let me know if it makes any difference. You may need to send us something we can test locally if this issue persists.

Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Josue
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or