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.:
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:
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:
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:
"application/msword" these are called Multiple Internet Mail Extensions (MIME) types.