此外,这是类似的问题,我找不到解决我的问题的方法。我对 JavaScript 也不是很熟练。
我创建了一个 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();
}
如果文件存在,它将打开打开/保存对话框,并且单击任何选项后选项卡将关闭。但是,如果文件不存在,则会显示警报,并且空白选项卡不会消失。
单击 gridView 中的此链接后开始下载:
<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>");
它应该可以工作,除非它被浏览器上的某些安全设置阻止。
关于c# - 调用 ashx 处理程序后关闭浏览器中的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423862/