调用ashx处理程序后关闭浏览器中的选项卡
本文关键字:浏览器 选项 ashx 处理 程序 调用 | 更新日期: 2024-07-23 03:56:43
此外,这是一个类似的问题,我找不到解决问题的方法。我对JavaScript也不是很熟练。
我创建了一个用于从sharepoint站点下载文件的ashx处理程序。当数据库中存在文件时,一切正常。若文件不存在,我应该弹出警报。
在ashx我创建:
var r = context.Response;
var attachmentID = context.Request.QueryString[QueryKeyID];
int id = 0;
if (!String.IsNullOrEmpty(attachmentID))
{
id = Convert.ToInt32(attachmentID);
DocKey k = new DocKey() { id = id };
DocImage od = MyWebService.GetDocImage(k);
String newFile = "document.doc";
r.ContentType = GetMimeTypeByFileName(newFile);
r.AppendHeader("Content-Type", GetMimeTypeByFileName(newFile));
r.AppendHeader("content-disposition", "attachment; filename=" + newFile);
r.BufferOutput = false; //Stream the content to the client, no need to cache entire streams in memory...
r.BinaryWrite(od.image);
r.End();
}
else
{
r.Write("<script type='text/javascript'>alert('no doc');</script>");
r.End();
}
如果文件存在,则打开"打开/保存"对话框,单击任何选项后关闭选项卡。但如果文件不存在,则会显示警报,并且空白选项卡不会消失。
点击网格中的链接后开始下载查看:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HiddenField ID="hdnkey" runat="server" Value='<%# getKey(Eval("Key")) %>' />
<a href="/_LAYOUTS/UserHandler/AttachmentHandler.ashx?ID='<%# getKey(Eval("Key")) %>'" target="_blank">download</a>
</ItemTemplate>
</asp:TemplateField>
我想在警报后自动关闭空白选项卡。
试试这个:
r.Write("<script type='text/javascript'>alert('brak dokumentu w bazie');window.close();</script>");
除非被浏览器上的某些安全设置阻止,否则它应该可以工作。
您不能这样做,因为您正在打开一个url,一个新的请求。
即使你有以下代码
r.AppendHeader("Content-Type","text/html");
r.Write("<script type='text/javascript'>alert('no doc');</script>");
此代码将在现有窗口中显示alert in new window
和。
在网格中呈现链接时,最好检测文档是否存在。如果不可用,则显示Not Found
文本
context.Response.Write("<script type='text/javascript'>function closeCurrentTab(){var conf=confirm('Are you sure, you want to close this tab?');if(conf==true){close();}}</script>");
context.Response.Write("<script type='text/javascript'>closeCurrentTab();</script>");