码迷,mamicode.com
首页 > Web开发 > 详细

ajax文件上传-FormData()

时间:2018-08-02 00:03:42      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:jquer   OLE   type   end   ajax文件上传   com   log   回调   请求   

HTML:

 1     <form action="">
 2     
 3         <input type="file" id="file1" name="">
 4     
 5         <br>
 6     
 7         <input type="file" id="file2" name="">
 8     
 9         <br>
10     
11         <input type="button" value="保存">
12     
13     </form>

JS:

        $("input[type=‘button‘]").on(‘click‘, upfile);

        /**
         * [upfile 文件上传]
         * @return {[Object]} [成功回调]
         */
        function upfile() {
            
            var formData = new FormData();

            formData.append("接收字段1", document.getElementById(‘file1‘).files[0]);
            // console.log(document.getElementById(‘file1‘).files[0]);
            
            formData.append("接收字段2", document.getElementById(‘file2‘).files[0]);
            // console.log(document.getElementById(‘file2‘).files[0]);

            $.ajax({

                url: ‘接口地址url‘,

                type: ‘POST‘,

                data: formData, // 上传formdata封装的数据包

                dataType: ‘JSON‘,

                cache: false, // 不缓存

                processData: false, // jQuery不要去处理发送的数据

                contentType: false, // jQuery不要去设置Content-Type请求头

                success: function(data) { // 成功回调

                    console.log(data);

                }

            });
        
        }

 成功后的效果:

技术分享图片

 

ajax文件上传-FormData()

标签:jquer   OLE   type   end   ajax文件上传   com   log   回调   请求   

原文地址:https://www.cnblogs.com/lprosper/p/9404245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!