Ajax | Form 提交图片文件

 <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: false, //上传文件不需要缓存
                data: formData,
                processData: false, // 告诉jQuery不要去处理发送的数据
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                       },
                       error : function(XMLHttpRequest, textStatus, errorThrown) {
      
                  }
                });
                //if($.browser.msie){
                //window.uploadForm.submit();
                //}else{
                //$("#uploadForm").submit();
                //}
            });
    </script>

猜你喜欢

转载自www.cnblogs.com/ya-jun/p/12553640.html