我正在让用户在锚标记单击时下载文件。在其 onclick 事件中,我使用 __doPostBack 部分回发页面以返回用户可下载的文件。
现在我面临的问题是,当找不到文件时,页面重新加载并且没有设置 ajax 调用。因此,简而言之,当浏览器在 _doPostBack 函数之后期待响应但没有得到响应时,它会重新加载页面但 $.ajax 方法不会运行。我错过了什么?我应该对响应做些什么吗?或者有什么办法可以取消响应?请帮忙。
例如,锚标签是
<a onclick="javascript:__doPostBack('AnnouncementAttachmentDownload','Ch 7 -Software Design2010.doc')">Ch 7 -Software Design2010.doc</a>
服务器端代码是
protected void Page_Load(object sender, EventArgs e)
{
SetPageTitles(ModuleNames.NotSpecified, null, false, true, true, false);
if (this.Request.Params["__EVENTTARGET"] == "AnnouncementAttachmentDownload")
{
string FileName = this.Request.Params["__EVENTARGUMENT"].ToString();
string destinationPath = Server.MapPath("~/" + System.Web.Configuration.WebConfigurationManager.AppSettings["AnnouncementAttachmentsPath"]).ToString() + "\\";
if (System.IO.File.Exists(destinationPath + FileName))
{
Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
Response.TransmitFile(destinationPath + FileName);
Response.End();
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "NewScript", "alert('The requested file could not be found, please contact portal.production.support');", true);\\DOESNT WORK
}
}
}
else 部分中的行,它只显示消息,但页面也会在此之后加载。