使用formdata的多文件上传  废话少说 直接上代码

1 JS部分

//选择文件后的处理
function handleFileSelect()
{
var exerciseid=$("#exerciseid").val();
var workerid=$("#workerid").val();
var pic_kind=$("#pic_kind").val();
//var pic_file = $('#pic_file')[0].files[0]; var formData = new FormData(); //formData.append("pic_file",$('#pic_file')[0].files[0]); var filesize=$('#pic_file')[].files.size; alert(filesize); for(var i=; i<$('#pic_file')[].files.length;i++){
alert($('#pic_file')[].files[i].size);
formData.append('file[]', $('#pic_file')[].files[i]);
} formData.append("pic_kind", pic_kind);
formData.append("exerciseid", exerciseid);
formData.append("workerid", workerid); $.ajax({
url: "{:U('Publishset/newUpload')}",
dataType:'json',
type:'POST',
data: formData,
processData : false, // 使数据不做处理
contentType : false, // 不要设置Content-Type请求头
xhr: function(){ //获取ajaxSettings中的xhr对象,为它的upload属性绑定progress事件的处理函数
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ //检查upload属性是否存在
//绑定progress事件的回调函数
myXhr.upload.addEventListener('progress',aprogressHandlingFunction, false);
}
return myXhr; //xhr对象返回给jQuery使用
},
success: function(data){ $('#ap').val();
$('#aprogress').html(''); showExerciseImage(exerciseid);
//if (data) { // alert('上传成功!');
//} },
error:function(response){
console.log(response);
}
}); } //上传进度回调函数:
function aprogressHandlingFunction(e) {
if (e.lengthComputable) {
$('#ap').attr({value : e.loaded, max : e.total}); //更新数据到进度条
var percent = e.loaded/e.total*;
$('#aprogress').html(percent.toFixed() + "%");
}
}

2 html 部分

 <input class="file" id="pic_file" multiple type="file" onchange="handleFileSelect(event)" style="display:none;">

3 PHP部分

 public function newUpload()
{ //上传配置
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize= ;// 设置附件上传大小
$upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath='./uploads/inittestimg/'; // 设置附件上传根目录 $upload->savePath=''; // 设置附件上传(子)目录 //上传文件
$info=$upload->upload(); //参数获取
$exerciseid=$_POST["exerciseid"];
$kind=$_POST['pic_kind'];
$workerid=$_POST['workerid']; if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功 获取上传文件信息 $images = M('initimages'); //循环入库
foreach($info as $file){
$imgarr['exerciseid']=$exerciseid;
$imgarr['src']='./uploads/inittestimg/'.$file['savepath'].$file['savename'];
$imgarr['kind']=$kind;
$imgarr['lastreadtime']=time(); //文件名
//$imgarr['orderid']=str_replace(strrchr($info['pic_file']['name'], "."),"",$info['pic_file']['name']);
$imgarr['orderid']=$file['name']; $images->add($imgarr);
} } echo ;
}

thinkphp3.2.3使用formdata的多文件上传的更多相关文章

  1. Java Web使用Html5 FormData实现多文件上传

    前一阵子,迭代一个线上的项目,其中有一个图片上传的功能,之前用的ajaxfileupload.js来实现上传的,不过由于ajaxfileupload.js,默认是单文件上传(虽然可以通过修改源码的方法 ...

  2. jQuery用FormData对象实现文件上传以及如何通过ajax下载文件

    之前在Vue的项目里面用到过文件上传,封装好的组件用起来比较顺手,查询Element-UI文档,十八般武器样样都有,一顿操作猛如虎,一看--跑偏了(⊙o⊙)-,我的意思就是用框架实现比较简单,但是如果 ...

  3. WebApi实现Ajax模拟Multipart/form-data方式多文件上传

    前端页面代码: <input type="file" class="file_control" /><br /> <input t ...

  4. multipart/form-data请求与文件上传

    要上传文件,需要用post方法,并且设置enctype为multipart/form-data. <form action="/upload" method="po ...

  5. multipart/form-data请求与文件上传的细节

    <!DOCTYPE html><html><head lang="en">  <meta charset="UTF-8" ...

  6. vue axios 与 FormData 结合 提交文件 上传文件

    ---再利用Vue.axios.FormData做上传文件时,遇到一个问题,后台虽然接收到请求,但是将文件类型识别成了字符串,所以,web端一直报500,结果是自己大意了. 1.因为使用了new  F ...

  7. tp5结合FormData实现ajax文件上传

    或者使用: 下面使用jquery.form.js的表单插件来提交表单

  8. FormData+Ajax 实现多文件上传 学习使用FormData对象

    FormData对象是为序列化表以及创建与表单格式相同的数据(当然是用于XHR传输)提供便利. 今天我们使用dropzone和FormData实现多文件上传功能. var SAMP = null; / ...

  9. vue使用formData进行文件上传

    本文为博主原创,未经允许不得转载 1.vue页面 <ux-form ref="formRef" layout="vertical"> <ux- ...

随机推荐

  1. ios--NavigationViewController跳转、返回传值

      使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面,这样的话,下一页面同样在NavigationViewContr ...

  2. linux下网卡捆绑

    七种bond模式说明:mod=0:(balance-rr) Round-robin policy(平衡抡循环策略)mod=1:(active-backup) Active-backup policy( ...

  3. Linux CentOs基本命令

    基本操作(命令模式下) yy --复制光标所在行 nyy --n为数字,复制光标所在向下n行 p --粘贴到光标的下一行 P --贴在光标的上一行 G --光标移到文件末尾 gg --光标移到文件头 ...

  4. Linux下JDK中文字体乱码

    java生成图片的时候用到字体,但是liunx系统没有这些字体需要把C:\Windows\Fonts 上传到/usr/local/jdk1.8.0_171/jre/lib/fonts 重启tomcat ...

  5. C# 集合根据属性去重筛选

    1.单个属性去重筛选 //去重筛选 var ChgDtlVoList = datas.Where((x, i) => datas.FindIndex(z => z.ChgId == x.C ...

  6. 学习input

    认识input: 在网页中,我们经常都会遇到一些交互页面,比如登录.注册.评论等页面.你知道在html中用的是那些标签吗?今天我们要学习的就是其中最主要的一个标签,即<input>标签. ...

  7. 阿里邮箱地址,smpt

    企业邮箱的POP3.SMTP.IMAP地址是什么? 企业邮箱POP.SMTP.IMAP地址列表如下: (阿里云邮箱web端通用访问地址:https://qiye.aliyun.com/),客户端推荐以 ...

  8. linux-部署1

    0.python安装 ubuntu16.04默认:安装了python2.7和python3.5: Ubuntu18.04默认:只有python3.6.8 下面是针对16.04: python/pyth ...

  9. Pyppeteer

    pyppeteer模块的基本使用 引言 Selenium 在被使用的时候有个麻烦事,就是环境的相关配置,得安装好相关浏览器,比如 Chrome.Firefox 等等,然后还要到官方网站去下载对应的驱动 ...

  10. Spring IOC小记

    1. What IOC (Inversion Of Control,控制反转)与DI(Dependency Injecion,依赖注入) 用于对象间解耦,如在以前若对象A依赖B则需要在A中负责B的创建 ...