【问题标题】:How to make jQuery loader before server responding for download a file如何在服务器响应下载文件之前制作 jQuery 加载器
【发布时间】:2015-01-07 12:15:05
【问题描述】:

我是法国人,抱歉我的英语不好。

我有一个为我生成文件的慢速 ASP.NET 方法:

public ActionResult Method([Bind(Prefix = "Id")] Guid id)
{
    //Long work
    return File(data, "application/pdf", doc.Name + ".pdf");
}

在我的 cshtml 文件中有一个按钮(我使用 Razor):

<a title="Télécharger" onclick="DownloadFile('@entity.Id')"/>

还有脚本方法:

<script>
    function DownloadFile(id) {
        var url = '@Url.Action("Method", "TheController", new { id = "Id" })'.replace('Id', id);
        $("#divLoading").show();
        $.post(url, null,
        function (data) {
            $("#divLoading").hide();
        });
    }
</script>

div 加载:

<div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px;
    top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001;
    opacity: .8; filter: alpha(opacity=70);display:none" >
    <p style="position: absolute; top: 30%; left: 45%; color: White;">
        Génération du document...<img src="../../Content/img/ajax-loader.gif">
    </p>
</div>

调用了C#方法,divLoading出现又消失,但是导航器没有下载文件。

如果我写: 这是工作。 但是装载机不存在。我想要。

我的问题不是如何在下载文件时制作加载器,而是只在下载开始之前显示加载器。

谢谢。

【问题讨论】:

    标签: jquery asp.net razor download loader


    【解决方案1】:

    您不能直接这样做,因为响应不会发送到浏览器。

    作为替代方案,您可以执行 2 个这样的请求:

    public ActionResult Method([Bind(Prefix = "Id")] Guid id)
    {
        //Long work
    
        string newGuid = Guid.NewGuid().ToString();
        TempData[newGuid] = data;
    
        return Content(newGuid);
    }
    
    public ActionResult Download(string guid)
    {
        byte[] data = (byte[])TempData[guid];
    
        return File(data, "application/pdf", "teste.pdf");
    }
    

    以及脚本方法:

    function DownloadFile(id) {
        var url = '@Url.Action("Method")';
        $("#divLoading").show();
        $.get(url, 
                    function (data, textStatus, jqXHR)
                    {
                        $("#divLoading").hide();
                        window.location.href = '@Url.Action("Download")' 
                                                + '?guid=' + data;
                    }
               );
    }
    

    【讨论】:

    • 好的!我明白。在对我的代码进行任何调整之后,它就可以工作了。非常感谢你,再次为我的英语不好感到抱歉。
    猜你喜欢
    • 2015-03-17
    • 2013-10-17
    • 2021-02-04
    • 1970-01-01
    • 2014-04-08
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多