简体   繁体   中英

Download byte array file

I have file that i converted and inserted it to my DB:

byte[] file = File.ReadAllBytes(outputpath);
string filesting = Convert.ToBase64String(file);
//INSERT INTO DB

Then I pull it from the DB and try to download it.

byte[] bytes = Convert.FromBase64String(file);
HttpContext.Current.Response.ContentType = "application/force-download";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=labtest.avi");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();

But nothing is downloaded.

Why?

Try using Response.BinaryWrite() and getting rid of the MemoryStream. The following code worked in my test (though I loaded a file from a resx resource, but as a byte array):

Response.ContentType = "application/force-download";
Response.AppendHeader("Content-Disposition", "attachment;filename=labtest.avi");

byte[] bytes = Convert.FromBase64String(file);
Response.BinaryWrite(bytes);

Response.End();

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