简体   繁体   中英

How to Convert BinaryData to File in C#?

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.

您可以尝试使用此示例来保存PDF文件。

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

`File.WriteAllBytes("file.pdf", binaryData);` 

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

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

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM