简体   繁体   English

发布jsp时显示动画gif

[英]Displaying animated gif when posting jsp

I want to display an animated gif to show the status of loading a jsp page when posting to the server. 我想显示一个动画gif,以显示发布到服务器时加载jsp页面的状态。 Here is my code: 这是我的代码:

        <form id="form" name="form" action="symptom" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">
            <input id="file" type="file" name="file" style="width: 470px; "/>
            <br><br>
            <input id="upload" type="submit" value="Upload" />                
        </form> 
        <br> <img src="img/loading.gif" id="loading" name="loading" style="display: none" height="42" width="42" > <br>
 <script>
        $.ajax({
        type: "POST",
        url: "symptom",
        data: {
            name: $("#form").val()
        },
        beforeSend:function(){
           $("#loading").show();
        },
        success:function(data){          
        },
        error:function(){
        }
    });
</script> 

When I click the submit button, the page was supposed to display the gif image but nothing is displayed. 当我单击提交按钮时,该页面应该显示gif图像,但什么也不显示。

Submitting an HTML form with jQuery is easy, but submitting an HTML Form with File uploads(multipart/form-data) is not straight forward. 使用jQuery提交HTML表单很容易,但是使用文件上传(multipart / form-data)提交HTML表单并不简单。

In jQuery, there is no direct API submit a multipart/form-data form. 在jQuery中,没有直接的API提交multipart/form-data表单。

There are different techniques doing so: 有不同的技术可以这样做:

For modern browsers supporting HTML5 you can use this code: 对于支持HTML5的现代浏览器,您可以使用以下代码:

//Callback handler for form submit event
$("#form").submit(function(e)
{
    var formObj = $(this);
    var formURL = formObj.attr("action");
    var formData = new FormData(this);
    $.ajax({
        url: formURL,
        type: 'POST',
        data:  formData,
        mimeType:"multipart/form-data",
        contentType: false,
        cache: false,
        processData:false,
        beforeSend:function(){
            $("#loading").show();
        },
        success: function(data, textStatus, jqXHR)

        {
            $("#loading").hide();
        },
        error: function(jqXHR, textStatus, errorThrown) {}          
    });
    e.preventDefault(); //Prevent Default action. 
    e.unbind();
}); 
$("#form").submit(); //Submit the form

In supporting old browsers as well you can use a technique by submitting hidden iframes. 在支持旧版浏览器中,您也可以通过提交隐藏的iframe使用一种技术。

For more info you can see this detailed tutorial 有关更多信息,请参见此详细教程。

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

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