Jquery form plugin file upload

后端 未结 1 1228
北恋
北恋 2020-12-28 11:58

i have a problem with jquery form plugin. I try to upload a file asynchronously but it does not submit the form. html markup and javascript code are like below



        
相关标签:
1条回答
  • 2020-12-28 12:23

    In short:

    <input type="file" />
    

    Cannot be submitted via ajax, it has to be a full postback. Traditionally you use an iFrame for this if you want AJAX style behavior. I've used a few solutions to this, without knowing what platform you're on, SWFUpload is usually a good option.

    Here's a full document example of a fix:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript" src="Javascript/jquery-1.3.2.js"></script>
        <script type="text/javascript" src="Javascript/jquery.form.js"></script>
        <script type="text/javascript">
            $(function() {            
              $('#fileUploadForm').ajaxForm({                 
                beforeSubmit: ShowRequest,
                success: SubmitSuccesful,
                error: AjaxError                               
              });                                    
            });            
    
            function ShowRequest(formData, jqForm, options) {
              var queryString = $.param(formData);
              alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
              return true;
            }
    
            function AjaxError() {
              alert("An AJAX error occured.");
            }
    
            function SubmitSuccesful(responseText, statusText) {        
              alert("SuccesMethod:\n\n" + responseText);
            }    
        </script>
    </head>
    <body>
        <form id="fileUploadForm" method="POST" action="Default.aspx" enctype="multipart/form-data">
          <input type="text" name="filename" />
          <input type="file" id="postedFile" name="postedFile" />
          <input type="submit" value="Submit" />
        </form>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题