Click here to Skip to main content
13,045,379 members (92,870 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I m storing files(word,excel,pdf) files into sqlserver, and while downloading file we are using below code:-

for (int i = 0; i < file.Tables[1].Rows.Count; i++)
                   Byte[] data = (Byte[])file.Tables[1].Rows[i]["FileBlob"];
                   string s = System.Text.UTF8Encoding.UTF8.GetString(data);
                   byte[] bytearray = (data);
                   System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                   //Response.Write( encoding.GetString(bytearray)) ;
                   string filename = file.Tables[1].Rows[i]["FileName"].ToString();
                   test.Text = filename + "<\br>";
                   string path = dir + "/" + filename;
                   var file1 = new FileInfo(Path.Combine(dir.FullName, filename));
                   if (!file1.Exists) // you may not want to overwrite existing files
                       using (Stream stream = file1.OpenWrite())
                       using (StreamWriter writer = new StreamWriter(stream))
                   zip.AddFile(path, "Files");

while writing byte data to excel file we are getting file corrupted. when we use below code:-

above line is working for text file and able to display data in text file the problem is with word and excel and pdf file.
Posted 1-Feb-13 0:12am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Why are you converting the file content from its native form into a string? That will destroy the Excel data.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100