如何下载文件时,显示和删除加载.gif注意? [英] How to display and remove a loading .gif when downloading a file?

查看:96
本文介绍了如何下载文件时,显示和删除加载.gif注意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,点击时,需要提交使用POST表单下载文件。 [修改,以澄清:下载动态通过基于隐藏表单内的值(这是我通过jQuery的变化,根据不同的按钮点击)PHP脚本生成的文件。有问题的文件是一个Excel文件。我想preFER不要使用,如果可能的GET,因为我不希望人们直接链接到的文件。]

I have a button that, when clicked on, needs to download a file by submitting a form using POST. [Edit to clarify: The file being downloaded is dynamically generated via a PHP script based on values within the hidden form (which I change via jQuery, depending on the button clicked). The file in question is an Excel file. I would prefer not to use GET if possible, since I don't want people to link directly to the file.]

我无法使用 $。交提交表单,因为这不工作,我想。同时提交发生细,它不会触发在浏览器中下载对话框。据我所知,该文件的内容现在在JavaScript中存在的,而且也没有办法(据我所知),可以让你从JavaScript发送此浏览器下载。因此,其他解决方案(例如,<一个href="http://stackoverflow.com/questions/3180323/how-do-i-use-jquery-or-ajax-to-swap-the-background-image-after-a-form-is-submitte">How我使用jQuery或Ajax来交换的背景图像后,提交表单?)将不会在这种特殊情况下正常工作。

I am unable to use $.post to submit the form because this does not work as I would like. While the submission happens fine, it doesn't trigger the download dialogue box in the browser. As I understand it, the contents of the file now exist within JavaScript, and there's no way (that I know of) that allows you to send this to the browser from JavaScript for download. So, other solutions (e.g. How do I use jQuery or Ajax to swap the background image after a form is submitted?) won't work in this particular case.

解决方案我发现是使用 $(#someform)提交(); 来触发一个隐藏的表单当按钮被点击。该网页不刷新,并且保存对话框弹出在屏幕上。

The solution I found was to use the $("#someform").submit(); to fire a hidden form when the button was clicked on. The page doesn't reload, and the save dialogue pops up on screen.

例如code:

$( "#button" ).click(
    function() {
        $(this).removeClass( "button" );
        $(this).addClass( "loading" );
        $( "#form" ).submit();  
    }
);

不过,因为它可能需要几秒钟才能生成文件,我想显示一个加载动画GIF给用户。现在,这presents,我可以显示动画中的问题,但已提交表单后,我无法删除的动画。

However, since it could take several seconds to generate the file, I wanted to display a loading animation gif to the user. This now presents the problem that I can display the animation, but I cannot remove the animation after the form has been submitted.

如果不能使用这种方法做,有没有更好的解决方案,有人建议?

If it can't be done using this method, is there a better solution someone can suggest?

推荐答案

我已经张贴在回答这个问题演示如何做到这一点。观看演示在 http://tomsfreelance.com/stackoverflow/imageDownloadLoading/main.php 并在 http://tomsfreelance.com/stackoverflow/imageDownloadLoading/

I've posted an answer in this question that demonstrates how to do this. See the demo at http://tomsfreelance.com/stackoverflow/imageDownloadLoading/main.php and source files at http://tomsfreelance.com/stackoverflow/imageDownloadLoading/

这篇关于如何下载文件时,显示和删除加载.gif注意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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