Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.89/5 (4 votes)
See more:
I already convert the file to byte array and save in the MYSQL Database.
I need to convert the byte array to file to send the file to another program.

Thanks
Posted
Comments
joshrduncan2012 13-Aug-13 12:08pm    
So what's your question?
David_Wimbley 13-Aug-13 12:10pm    
" I need to convert the byte array to file to send the file to another program."

You would need to know your file extension. Im assuming you either have it in the table or its the same file extension no matter what (say pdf).

1) Read/Write a file to a byte array and back to file

C#
//Read file to byte array

FileStream stream = File.OpenRead(@"c:\path\to\your\file\here.txt");
byte[] fileBytes= new byte[stream.Length];

stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
//Begins the process of writing the byte array back to a file

using (Stream file = File.OpenWrite(@"c:\path\to\your\file\here.txt"))
{
   file.Write(fileBytes, 0, fileBytes.Length);
}


Unless i completely misunderstood your question that should be what you are asking for i believe.
 
Share this answer
 
Comments
Member 2368219 12-Jun-14 16:58pm    
David,
Thanks a lot for your solution, it was of great help.
Easiest solution is:
File.ReadAllBytes[^] to get the byes and File.WriteAllBytes[^] to write it back to a file.
 
Share this answer
 
 
Share this answer
 
public ActionResult Document(int id)
{
var obj = new CEATLMSEntities().LeaveDocuments.Where(c => c.Id == id).FirstOrDefault();
string[] stringParts = obj.FName.Split(new char[] { '.' });
string strType = stringParts[1];
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment; filename=" + obj.FName);
var asciiCode = System.Text.Encoding.ASCII.GetString(obj.Document);
var datas = Convert.FromBase64String(asciiCode.Substring(asciiCode.IndexOf(',') + 1));
//Set the content type as file extension type
Response.ContentType = strType;
//Write the file content
this.Response.BinaryWrite(datas);
this.Response.End();
return new FileStreamResult(Response.OutputStream, obj.FType);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900