jac*_*own 1 c# asp.net httpresponse
我有已转换的文件并将其插入到我的数据库中:
byte[] file = File.ReadAllBytes(outputpath);
string filesting = Convert.ToBase64String(file);
//INSERT INTO DB
Run Code Online (Sandbox Code Playgroud)
然后我从数据库中提取它并尝试下载它。
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();
Run Code Online (Sandbox Code Playgroud)
但没有下载任何内容。
为什么?
尝试使用Response.BinaryWrite()和摆脱 MemoryStream。以下代码在我的测试中有效(尽管我从 resx 资源加载了一个文件,但作为字节数组):
Response.ContentType = "application/force-download";
Response.AppendHeader("Content-Disposition", "attachment;filename=labtest.avi");
byte[] bytes = Convert.FromBase64String(file);
Response.BinaryWrite(bytes);
Response.End();
Run Code Online (Sandbox Code Playgroud)