0

Actually I have saved some files like .pdf,.txt,.doc,.xls by converting them to <Binary data> in my SQL SERVER DB through FileUploadControl. Now, I want to Convert the <Binary data> back to Normal and give an option for user to Download (or) View that data.

I have tried some thing like this for the .txt files

var filedata = (from xx in VDC.SURVEY_QUESTION_REPLIES
                                    where xx.ID == FileID
                                    select xx).FirstOrDefault();

string fileextension = filedata.FILE_EXTENSION.ToString();
string fileName = filedata.ANSWER_TEXT.ToString() + fileextension;
Byte[] bytes = (Byte[])filedata.FILE_DATA;
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

I am getting an error like :

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
RealSteel
  • 1,871
  • 3
  • 37
  • 74

2 Answers2

0

You can try this sample for saving PDF file.

byte[] binaryData = (byte[])filedata.FILE_DATA;

`File.WriteAllBytes("file.pdf", binaryData);` 
Ishtiaq
  • 980
  • 2
  • 6
  • 21
0

Is the datatype System.Data.Linq.Binary then you could do something like:

byte[] bytes = filedata.FILE_DATA.ToArray();

Regards.