Introduction
In this article, let us see how to convert a file content to a byte array, restore the original content from the byte array, and display it in its original file formats such as pdf, doc, xls, rtf, jpeg, png, etc.
Convert a file content to a byte array
Step 1. Create an ASP.Net application and add a class Document.
public class Document
{
public int DocId { get; set; }
public string DocName { get; set; }
public byte[] DocContent { get; set; }
}
Step 2. Create a format doc/pdf/rtf file and convert the file content to a ByteArray using the following method. Then create an object of type Document and assign the Docname and DocContent property values from the filename and filecontent.
public Document FileToByteArray(string fileName)
{
byte[] fileContent = null;
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
long byteLength = new System.IO.FileInfo(fileName).Length;
fileContent = binaryReader.ReadBytes((Int32)byteLength);
fs.Close();
fs.Dispose();
binaryReader.Close();
Document = new Document();
Document.DocName = fileName;
Document.DocContent = fileContent;
return Document;
}
Convert a byte array to file content
Step 3. Now we need to revert the byte array to the file content and display it to the user with download options. For that, I am using another method, ShowDocument, as below.
private void ShowDocument(string fileName, byte[] fileContent)
{
//Split the string by character . to get file extension type
string[] stringParts = fileName.Split(new char[] { '.' });
string strType = stringParts[1];
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
//Set the content type as file extension type
Response.ContentType = strType;
//Write the file content
this.Response.BinaryWrite(fileContent);
this.Response.End();
}
Step 4. In the page_load event, I have the following code: read the contents from the file and convert it to a ByteArray using the FileToByteArray method, and convert it back to content using the ShowDocument method.
protected void Page_Load(object sender, EventArgs e)
{
FileToByteArray("C:\\Users\\santhosh.kumar\\Documents\\Test for rht format.rtf");
ShowDocument(Document.DocName, Document.DocContent);
}
Summary
This article taught us how to convert files to Byte Arrays and Byte Arrays to Files. For the complete source code, please find the attached solution.
If you require any clarification/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about Programming or to explore more technologies.
Thanks for reading, and I hope you like it.

aneesh dathPosted Jul 25, 2024, 6:38 AM
Is reading of excel ( xlsx) really works this approach?
getnet AdanePosted Dec 9, 2022, 7:08 AM
What is Response AM getting an error also.? please any one help me !
Vie ViePosted May 3, 2019, 1:59 AM
What is Response? I'm getting an error in xamarin? Is it applicable in Xamarin? I need answer. Hoping to receive a reply son. Thanks!
sangram JadhavPosted Nov 15, 2018, 12:15 AM
I am trying to get data from Pdf having marathi font but output is not proper
gps.mcaPosted Dec 6, 2016, 2:18 PM
Please let us know how can we convert the byte date to excel file without showing excel editor.
Manoj KallaPosted Feb 17, 2016, 3:01 AM
Good one sir..
Gobi GPosted Mar 12, 2015, 3:41 AM
very nice
Rishikesh Kumar SinghPosted Aug 7, 2014, 2:30 AM
using your ShowDocument() we are downloading the word document..But what to do if i want to show content on webpage...please do help me in this scenario
tri nguyenPosted Jun 10, 2013, 9:23 AM
Hi Santhosh , very thanksfull about your post , but I have a problem , I need to convert byte array to a PDF file from a web service , can you tell me what I can do ! Thanks you :)