我想在我的应用程序中显示.mdb文件的下载弹出。我尝试使用此代码:
string path = " D:\New people metrix 7th april\People Metrix New Web\Bin\Inbox.mdb";
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(path));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.WriteFile(path);
HttpContext.Current.Response.End();
但是我得到了这个错误:
无法评估表达式,因为代码已优化或本机框架位于调用堆栈之上
这个bug真的杀了我!!! ...请帮帮我。
我说得对了...我把图像按钮放在更新面板中所以它不允许下载..谢谢这么多家伙帮助我...
答案 0 :(得分:3)
好吧,至少在设置之后调用Response.Clear()
看起来像是一个错误 - 尝试将其移至开头?
答案 1 :(得分:2)
sFilePath = “下载/ query.txt”; sFileName = “query.txt”
HttpContext.Current.Response.ContentType =“APPLICATION / OCTET-STREAM”; //由于这两行你会弹出来 String Header =“Attachment; Filename =”+ sFileName; HttpContext.Current.Response.AppendHeader(“Content-Disposition”,Header);
System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
HttpContext.Current.Response.WriteFile(Dfile.FullName);
HttpContext.Current.Response.End();
答案 2 :(得分:1)
我使用这样的东西:
string filePath = Server.MapPath("~/" + document.Path + "/" + document.Filename);
System.IO.FileInfo targetFile = new System.IO.FileInfo(filePath);
if(targetFile.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
Response.AddHeader("Content-Length", targetFile.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(targetFile.FullName);
}
使用的文档对象是我的一个容器,带有路径和文件名。
尝试一下,让我知道它是否有效,对我有用。
答案 3 :(得分:0)
我做这样的事我更喜欢BinaryWrite到WriteFile,而且我没有设置内容长度,因为如果你试图下载一个0字节的文件,它会抛出一些浏览器。
string path = @"D:\New people metrix 7th april\People Metrix New Web\Bin\Inbox.mdb"; FileInfo fileinfo = new FileInfo(path); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", string.Concat("attachment; filename=", path)); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.BinaryWrite(File.ReadAllBytes(fileinfo,.FullName)); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End();
答案 4 :(得分:0)
您的路径不会转义“\”字符。当您调用 Path.GetFileName 时,可能会抛出一个被捕获的异常,并在代码中的其他地方被忽略/翻译。
答案 5 :(得分:0)
Response.Clear(),似乎是这里的问题。
你可以尝试一下。
public static void ForcedDownload(HttpResponse response, byte[] buffer, string fileName) {
response.Clear();
response.Buffer=true;
response.AddHeader("content-disposition", string.Format(@"attachment;filename={0}", fileName));
response.Charset="";
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType="application/octet-stream";
response.BinaryWrite(buffer);
response.Flush();
response.End();
}