ASP.net View an image file in the browser

Murray Brown
Murray Brown used Ask the Experts™
on
Hi. In my ASP.net web app I am using the following code to view a pdf file. If I want to view an image file such as a jpg or png
file what code would I use? Thanks
    Protected Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
        Try
            Dim oSelectedFile As String
            If Me.ListBox1.SelectedItem.Text <> "" Then
                oSelectedFile = Me.ListBox1.SelectedItem.Text
            Else
                Exit Sub
            End If
            'Response.Clear()
            'Dim filePath As String = "~/Uploads/" & oSelectedFile
            'Response.ContentType = "application/pdf"
            'Response.WriteFile(filePath)
            'Response.TransmitFile(filePath)

            'Dim FilePath As String = Server.MapPath("piyushsir.pdf")
            Dim FilePath As String = Server.MapPath("~/Uploads/" & oSelectedFile)
            Dim User As New System.Net.WebClient()
            Dim FileBuffer As [Byte]() = User.DownloadData(FilePath)
            If FileBuffer IsNot Nothing Then
                Response.ContentType = "application/pdf"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
                Response.BinaryWrite(FileBuffer)
            End If

        Catch ex As Exception
            'Response.Write(ex.Message)
            Me.lblErrorDeleting.Text = ex.Message & " hr556"
        End Try
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Murray,

There are couple of ways.

1. You can check the extension of the file name i.e. the last 3 character of the file name - This is an easy way but does not make sure that the file is PDF or not
2. You use any of the PDF processing library, try to open the PDF in memory and if it gives an error - assume that it is the other type of file (Or use Image processing library to check if the file is an image or not) - This is bit tough but is a sure shot way.
3. You can check the header by reading couple of bytes and determine file type - I am not sure how accurate this method would be though.

Let me know which one would you like to go with.

Regards,
Chinmay.
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks Chinmay. What would I change in my code to view an image file such as a jpg?
I suppose these three lines would have to change
 
    Response.ContentType = "application/pdf"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
                Response.BinaryWrite(FileBuffer)

Open in new window

Chief Technology Ninja
Distinguished Expert 2018
Commented:
You are welcome Murray.
WOW.. 4 days..  I dunno how I missed this... so sorry for the delay

 Response.ContentType = "image/jpeg"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
                Response.BinaryWrite(FileBuffer)

Open in new window

Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial