简体   繁体   English

在调用ashx处理程序后关闭浏览器中的选项卡

[英]Closing the tab in browser after calling a ashx handler

Besides it is similar question I cannot find solution to my problem. 除了它是类似的问题,我无法找到解决我的问题。 I am also not very skilled with JavaScript. 我对JavaScript也不是很熟练。

I create a ashx handler for downloading files from sharepoint site. 我创建了一个ashx处理程序,用于从sharepoint站点下载文件。 All works fine when file in database exist. 当数据库中存在文件时,一切正常。 If file does not exist I should pop-up alert. 如果文件不存在,我应该弹出警报。

In ashx I create: 在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();

        }

If file exist it open open/save dialog and tab is closed after clicking any option. 如果存在文件,则打开打开/保存对话框,单击任何选项后关闭选项卡。 But if file does not exist alert is shown and the blank tab does not disappear. 但是如果文件不存在,则会显示警报,并且空白选项卡不会消失。

Download starts after clicking this link which is in gridView : 单击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>

I want to make auto cloasing the blank tab after alert. 我想在警报后自动修剪空白标签。

Try this: 尝试这个:

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

It should work unless it's blocked by some security setting on the browser. 它应该工作,除非它被浏览器上的某些安全设置阻止。

You cannot do like that, since you are opening a url, a new request. 你不能这样做,因为你打开一个网址,一个新的请求。

Even if you have the below code 即使您有以下代码

 r.AppendHeader("Content-Type","text/html");

 r.Write("<script type='text/javascript'>alert('no doc');</script>");

This code will show you the alert in new window and in the existing window. 此代码将alert in new window和现有窗口中显示alert in new window

It is better to detect a document exists or not at the time of rendering linkes in grid. 最好在网格中呈现链接时检测文档是否存在。 If not available, show Not Found text 如果不可用,请显示“ 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>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM