Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
HI,
 
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);
                   Response.Write(s);
                   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))
                       {
                         writer.Write(encoding.GetString(bytearray);
                          writer.Flush();
 
                       }
                   }
                   zip.AddFile(path, "Files");
               }
           }
 

while writing byte data to excel file we are getting file corrupted. when we use below code:-
writer.Write(encoding.GetString(bytearray);
 
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 1:12am

1 solution

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

Solution 1

writer.Write(encoding.GetString(bytearray);
Why are you converting the file content from its native form into a string? That will destroy the Excel data.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2014
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