从页面方法加载文件
我有一个 JavaScript 函数,它调用页面方法,如下所示:
function openFile(file) {
PageMethods.LoadFile(file);
}
[System.Web.Services.WebMethod]
public static void LoadFile(string fileName)
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/binary";
var filePath = @"C:\MyFiles\" + fileName;
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", fileName));
HttpContext.Current.Response.WriteFile(filePath);
HttpContext.Current.Response.End();
}
页面方法引发以下异常:
Microsoft JScript 运行时错误:Sys.Net.WebServiceFailedException:服务器方法“LoadFile”失败,出现以下错误:System.Threading .ThreadAbortException--线程被中止。
我该如何解决这个问题?
I have a JavaScript function which calls a page method as follows:
function openFile(file) {
PageMethods.LoadFile(file);
}
[System.Web.Services.WebMethod]
public static void LoadFile(string fileName)
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/binary";
var filePath = @"C:\MyFiles\" + fileName;
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", fileName));
HttpContext.Current.Response.WriteFile(filePath);
HttpContext.Current.Response.End();
}
The page method throws the following exception:
Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'LoadFile' failed with the following error: System.Threading.ThreadAbortException-- Thread was being aborted.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到此异常是因为您调用了 End() 并强制中止。
HttpContext.Current.Response.End();
只需将其删除即可。
参考:http://support.microsoft.com/kb/312629/
如果您用谷歌搜索:HttpContext.Current.Response.End
更新,
我现在看到您尝试发送文件,但您已经设置了本地磁盘的路径。将其更改为您的 http 地址。
You get this Exception because you have call the End() and you force the abort.
HttpContext.Current.Response.End();
Just remove it.
Reference : http://support.microsoft.com/kb/312629/
And if you google it: HttpContext.Current.Response.End
update
I see now that you try to send a file, but you have set the path to your local disk. Change that to your http address.