从JavaScript调用的HttpHandler [英] Call HttpHandler from javascript

查看:89
本文介绍了从JavaScript调用的HttpHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,一个简单的网页,其中通过JavaScript调用HttpHandler的。

I have a simple page with button which calls HttpHandler via JavaScript.

HttpHandler的获取大量文件,并将它们添加到一个zip文件,整理工作的zip文件将被添加到响应之后。

HttpHandler gets lots of files and adds them to a zip file, after finishing work zip file will be added to Response.

此操作可能需要几分钟的时间。我想整理的HttpHandler的工作后执行一些JavaScript函数。

This operation can take several minutes. I would like to execute some JavaScript function after finishing work of HttpHandler.

我该怎么办呢?

我的code:

<asp:Button ID="btnDownload" runat=server Text="Download" OnClientClick="Download()" />

<script type="text/javascript">
    function Download()
    {
        var url = 'FileStorage.ashx';
        window.open(url);            
    }
</script>

UPD 1:

我已经找到其他的解决办法。使用XMLHtt prequest。

I have found other solution. Using XMLHttpRequest.

code:

 <script type="text/javascript">
        var xmlHttpReq = createXMLHttpRequest();

        function Download() {
            var url = 'FileStorage.ashx';            
        xmlHttpReq.open("GET", url, false);
        xmlHttpReq.onreadystatechange = onResponse;
        xmlHttpReq.send(null);         
    }

    function onResponse() {
        if (xmlHttpReq.readyState != 4)
        { return; }        
        var serverResponse = xmlHttpReq.responseText;
        alert(serverResponse);        
     }

    function createXMLHttpRequest() {
        try { return new XMLHttpRequest(); } catch (e) { }
        try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
        try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
        alert("XMLHttpRequest not supported");
        return null;
    }
    </script>

在onResponse()我可以看到的responseText(二进制)我的zip文件。但是,我没有任何想法如何,我可以说浏览器下载工作的HttpHandler,如文件的结果。

In onResponse() I can see my zip file in responseText (binary). But I don't have any idea how I can say to browser to download result of working httphandler such as file.

任何想法?

推荐答案

我可以使用jQuery AJAX,然后成功,编写一个函数,会做你需要它力所能及的工作。加上AJAX可以显示用户,说装,让他们知道什么是实际处理的图标,而不是页面只是挂并没有什么出现的情况发生。

I would use JQuery AJAX and then on success, write a function that will do whatever work you need it to. Plus with AJAX you can show the user an icon that says loading so they know something is actually processing, instead of the page just hanging and nothing appearing to happen.

$.ajax({
  url: "FileStorage.ashx",
  context: document.body,
  success: function(){
     // Whatever you want to do here in javascript.
  }
});

这篇关于从JavaScript调用的HttpHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆