Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How to Display File on Browser using Byte Array

Author: Faraz
Download Source Code : 282_ByteArray.zip

In this article I will try to explain you the technique of displaying file ion the browser using Byte Array. Here I have created a sample application in .Net Framework 2.0.

Arrays are the simplest form, which contains the collection of objects. In object oriented programming (OOP), arrays provide great deal of flexibility to programmers in storing objects, strings, characters etc. In Microsoft .Net Framework you will find System.Array class, very core of .Net Framework and serves as the base class for all simple arrays in the Common Language Runtime (CLR) environment. To know and work more with collection object you will find System.Collection namespace which contains most of the classes and interfaces.

 

Here I will not talk about these collection classes and interface I will simply show you how you can get benefit from the byte arrays. As you know that you have the capability to move the file contents to a stream object e.g.:

 

Dim StreamObj As New System.IO.FileStream(("ReadFile.txt"), IO.FileMode.Open)

Using this object you can also move file contents to a Byte Array. To do so you first need to move contents to stream object and then to byte array. You can have more understanding of this function by considering following example. In this example, I have created a class named as FileManager(), in this class you will see one constructor and read only public property method:

 

Public Class FileManager

  Inherits System.Web.UI.Page

 

'Global String object

Dim m_FileManager As String

 

'Constructor the reads the file name

'and assign to string object

Public Sub New(ByVal fileName As String)

      m_FileManager = fileName

End Sub

 

'Property Builder the reads the file and store it in byte array

Public ReadOnly Property FileManager() As Byte()

Get

      'File Stream object

      Dim StreamObj As New System.IO.FileStream(Server.MapPath(m_FileManager), IO.FileMode.Open)

 

      'Create Buffer

      Dim buffer(StreamObj.Length) As Byte

 

      'Fill Buffer

      StreamObj.Read(buffer, 0, buffer.Length)

      StreamObj.Close()

 

  'Return Buffer Contents

  Return buffer

End Get

      End Property

End Class

On Page_load event simply you have to do is create a FileManager class object, pass file name, fill buffer and finally display contents on the web form:

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

 'Read file from server

 Dim fmObj As New FileManager("adidas.gif")

 

 'Fill Buffer

 Dim buffer() As Byte = fmObj.FileManager

 

 'Display Contents on web form

 Response.OutputStream.Write(Buffer, 0, Buffer.Length)

 

End Sub

Note you can display any file format on the web browser. But you have to take care of one thing that is when you are working with image file you don’t need to define content type but if you working with word format file or PDF or any other file then you have to define the Content Type:

Response.ContentType = "application/msword"

 

"application/msword" these are called Multiple Internet Mail Extensions (MIME) types.

Article Comments
I think this would help

Posted on 5/24/2006 12:39:35 PM by Hoo

Hi,
I used your method to show PDF files in browser, it works great.
But I have some FDF files that will merge data into source PDF files, how do I show FDF files in browser?
Thanks!

Qian

Posted on 9/19/2007 12:26:52 PM by Qian Yu

I want to know how to display any file type in a browser

Posted on 9/5/2008 4:29:11 AM by Subasini Patro

I think it would help.
we r developing an asp.net application.In this , pdf file is browse and stored into database as byte array.i want to show that pdf file after clicking imagebutton.its getting in the same window.but i want to show that in a new window..

Posted on 3/5/2010 6:51:04 AM by Deepu

I think it would help.
we r developing an asp.net application.In this , pdf file is browse and stored into database as byte array.i want to show that pdf file after clicking imagebutton.its getting in the same window.but i want to show that in a new window..

Posted on 7/19/2010 1:54:11 AM by sagar

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to compress file using .Net Framework 2.0

Disclaimer - Privacy
© 2002-2012 DevASP.net