Ajax | Form submit image files

 <form name="uploadForm" id="uploadForm" method="post" enctype="multipart/form-data"
          action="URL" target="uploadFrame">
        <p style="margin:10px 0;">
            上传图片: &nbsp;&nbsp;
            <input type="file" name="image" ID="image" />
            <input type="text" name="userId" ID="userId" value="" />
            <input type="button" id="fileSubmit" name="Submit" value="上传" />
            <iframe name="uploadFrame" id="uploadFrame" style="display:none;"></iframe>
        </p>
    </form>
    <script>

        $("#fileSubmit").click(function () {
            var formData = new FormData();
            formData.append('image', document.getElementById('image').files[0]);
            formData.append('userId',0);
            $.ajax({
                url:"URL",
                type: 'post',
                cache: to false , // upload file does not need to cache 
                Data: formData, 
                the processData: to false , // tell jQuery not to process data transmitted 
                contentType: to false , // tell jQuery not to set the Content-Type header request 
                success: function (data) { 
                       }, 
                       error: function (the XMLHttpRequest, textStatus, errorThrown) { 
      
                  } 
                }); 
                // . IF ($ .browser.msie) {
                 // window.uploadForm.submit ();
                 // } the else {
                 //$("#uploadForm").submit();
                //}
            });
    </script>

 

Guess you like

Origin www.cnblogs.com/ya-jun/p/12553640.html