【问题标题】:Closing the tab in browser after calling a ashx handler调用 ashx 处理程序后关闭浏览器中的选项卡
【发布时间】:2013-12-06 12:24:15
【问题描述】:

除了类似的问题,我找不到解决问题的方法。我对 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>

我想在警报后自动关闭空白标签。

【问题讨论】:

    标签: c# javascript asp.net httpresponse ashx


    【解决方案1】:

    试试这个:

    r.Write("<script type='text/javascript'>alert('brak dokumentu w bazie');window.close();</script>");
    

    它应该可以工作,除非它被浏览器上的某些安全设置阻止。

    【讨论】:

    • 嘿,我以前试过这个,但我想我做错了什么。添加此行后,在我发出警报后,我从浏览器收到警告说应用程序尝试关闭选项卡(您要关闭此选项卡吗?)。我可以避免来自浏览器 (IE11) 的警告吗?
    • r.Write("");并在警报新标签关闭后
    【解决方案2】:

    你不能那样做,因为你正在打开一个 url,一个新的请求。

    即使你有下面的代码

     r.AppendHeader("Content-Type","text/html");
    
     r.Write("<script type='text/javascript'>alert('no doc');</script>");
    

    此代码将在现有窗口中显示alert in new window

    最好在网格中渲染链接时检测文档是否存在。如果不可用,显示Not Found 文字

    【讨论】:

    • 我想检测它并防止打开新标签会更好。现在我使用 LittleSweet 的帮助并将一行更改为: r.Write("");并在警报新标签关闭后。
    【解决方案3】:
    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>");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2022-06-27
      • 1970-01-01
      • 2015-12-27
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多