1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JQ插件ajaxFileUpload php实现图片 数据同时上传 – 云翳1895

JQ插件ajaxFileUpload php实现图片 数据同时上传 – 云翳1895

时间:2018-12-11 14:08:04

相关推荐

JQ插件ajaxFileUpload php实现图片 数据同时上传 – 云翳1895

php教程|php手册

JQ插件ajaxFileUpload、php实现图片,数据同时上传 – 云翳1895

php教程-php手册

代码结构如下:

mysql5.6源码安装包下载,vscode删除整列,ubuntu 不锁屏,tomcat 默认,sqlite 搜索,网页设计一屏的高度,空间数据库 建库流程,outlook imap服务器,爱字体插件,国内主流web前端框架,自动爬虫,php div,SEO的意思,springboot变量下划线,灵动标签调用时间,免费企业网站源代码,网页对话框 精易vip,网页编辑模板,怎么将下载的模版网页传到后台,jsp左边菜单 右边页面,动软 后台管理系统 代码,易语言 获取程序基址lzw

flexpaper 源码,vscode粘贴代码会错位,ubuntu源网址,tomcat开始不了了,爬虫春雨医生,php单点登录原理,昌邑关键词seo优化,php全屏大气企业网站源码2,生鲜o2o网站模板lzw

1、HTML代码,没必要解释了。

网络在线考试系统源码,html生成器vscode,ubuntu 最低内存,go服务在tomcat,robots 爬虫漏洞,php面向对象理解,代理代发如何推广seo顾问,政府网站代码模板,齐博门户黄页模板lzw

1 2 345文件上传6789 $(function(){10 $(#ti).click(function(){11var data = { name: my name, description: short description } 12$.ajaxFileUpload({13 url: up.php,14 secureuri: false,15 data: data,16 fileElementId: upf,17 dataType: json,18 success: function (data) {19 // var datejson=eval(data);20 //console.log(data[0].path_name)21$(#im).append(\) 22 //console.log(\)23 },24 error: function (data) {25console.log(data)26 }27});28 29 })30 31 })32 333435<!--36-->37 38 39 40

41 42

43 4445 46

2、关于ajaxFileUpload插件,在下面代码中如果你使用的是JQ1.9以上请复制1-12到你的ajaxFileUpload代码中,JQ在很早就废弃了handleError方法。注释关于代码的解释很清楚。

1 jQuery.extend({ 2handleError: function( s, xhr, status, e ) { 3 4 if ( s.error ) { 5s.error.call( s.context || s, xhr, status, e ); 6 } 7 8 9 if ( s.global ) { 10(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] ); 11 } 12 }, 13createUploadIframe: function (id, uri) {//id为当前系统时间字符串,uri是外部传入的json对象的一个参数 14 //create frame 15 var frameId = jUploadFrame + id; //给iframe添加一个独一无二的id 16 var iframeHtml = \; 25 jQuery(iframeHtml).appendTo(document.body); //将动态iframe追加到body中 26 return jQuery(# + frameId).get(0); //返回iframe对象 27}, 28createUploadForm: function (id, fileElementId, data) {//id为当前系统时间字符串,fileElementId为页面的id,data的值需要根据传入json的键来决定 29 //create form30 var formId = jUploadForm + id; //给form添加一个独一无二的id 31 var fileId = jUploadFile + id; //给添加一个独一无二的id 32 var form = jQuery(\); //创建form元素 33 if (data) {//通常为false 34 for (var i in data) { 35 jQuery(\).appendTo(form); //根据data的内容,创建隐藏域,这部分我还不知道是什么时候用到。估计是传入json的时候,如果默认传一些参数的话要用到。 36 } 37 } var oldElement = jQuery(# + fileElementId); //得到页面中的对象 38 var newElement = jQuery(oldElement).clone(); //克隆页面中的对象 39 jQuery(oldElement).attr(id, fileId); //修改原对象的id 40 jQuery(oldElement).before(newElement); //在原对象前插入克隆对象 41 jQuery(oldElement).appendTo(form); //把原对象插入到动态form的结尾处 42 //set attributes 43 jQuery(form).css(position, absolute); //给动态form添加样式,使其浮动起来, 44 jQuery(form).css( op, -1200px); 45 jQuery(form).css(left, -1200px); 46 jQuery(form).appendTo(ody); //把动态form插入到body中 47 return form; 48}, 49ajaxFileUpload: function (s) {//这里s是个json对象,传入一些ajax的参数 50 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout 51 s = jQuery.extend({}, jQuery.ajaxSettings, s); //此时的s对象是由jQuery.ajaxSettings和原s对象扩展后的对象 52 var id = new Date().getTime(); //取当前系统时间,目的是得到一个独一无二的数字 53 var form = jQuery.createUploadForm(id, s.fileElementId, (typeof (s.data) == undefined ? false : s.data)); //创建动态form 54 var io = jQuery.createUploadIframe(id, s.secureuri); //创建动态iframe 55 var frameId = jUploadFrame + id; //动态iframe的id 56 var formId = jUploadForm + id; //动态form的id 57 // Watch for a new set of requests 58 if (s.global && !jQuery.active++) {//当jQuery开始一个ajax请求时发生 59 jQuery.event.trigger("ajaxStart"); //触发ajaxStart方法 60 } var requestDone = false; //请求完成标志 61 // Create the request object 62 var xml = {}; if (s.global) 63 jQuery.event.trigger("ajaxSend", [xml, s]); //触发ajaxSend方法 64 // Wait for a response to come back 65 var uploadCallback = function (isTimeout) {//回调函数 66 var io = document.getElementById(frameId); //得到iframe对象 67 try {if (io.contentWindow) {//动态iframe所在窗口对象是否存在 68xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null; 69xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document; 70 } else if (io.contentDocument) {//动态iframe的文档对象是否存在 71xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null; 72xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document; 73 } 74 } catch (e) { 75 jQuery.handleError(s, xml, null, e); 76 } if (xml || isTimeout == "timeout") {//xml变量被赋值或者isTimeout == "timeout"都表示请求发出,并且有响应 77 requestDone = true; //请求完成 78 var status;try { 79status = isTimeout != "timeout" ? "success" : "error"; //如果不是“超时”,表示请求成功 80// Make sure that the request was successful or notmodified 81if (status != "error") { // process the data (runs the xml through httpData regardless of callback) 82 var data = jQuery.uploadHttpData(xml, s.dataType); //根据传送的type类型,返回json对象,此时返回的data就是后台操作后的返回结果 83 // If a local callback was specified, fire it and pass it the data 84 if (s.success) 85 s.success(data, status); //执行上传成功的操作 86 // Fire the global callback 87 if (s.global) 88 jQuery.event.trigger("ajaxSuccess", [xml, s]); 89} else 90 jQuery.handleError(s, xml, status); 91 } catch (e) { 92status = "error"; 93jQuery.handleError(s, xml, status, e); 94 }// The request was completed 95 if (s.global) 96jQuery.event.trigger("ajaxComplete", [xml, s]);// Handle the global AJAX counter 97 if (s.global && ! --jQuery.active) 98jQuery.event.trigger("ajaxStop");// Process result 99 if (plete)100plete(xml, status);101 jQuery(io).unbind();//移除iframe的事件处理程序102 setTimeout(function () {//设置超时时间103try {104 jQuery(io).remove();//移除动态iframe105 jQuery(form).remove();//移除动态form106} catch (e) {107 jQuery.handleError(s, xml, null, e);108}109 }, 100)110 xml = null111 }112 } // Timeout checker113 if (s.timeout > 0) {//超时检测114 setTimeout(function () {// Check to see if the request is still happening115 if (!requestDone) uploadCallback("timeout");//如果请求仍未完成,就发送超时信号116 }, s.timeout);117 } try { var form = jQuery(# + formId);118 jQuery(form).attr(action, s.url);//传入的ajax页面导向url119 jQuery(form).attr(method, POST);//设置提交表单方式120 jQuery(form).attr( arget, frameId);//返回的目标iframe,就是创建的动态iframe121 if (form.encoding) {//选择编码方式122 jQuery(form).attr(encoding, multipart/form-data);123 } else {124 jQuery(form).attr(enctype, multipart/form-data);125 }126 jQuery(form).submit();//提交form表单127 } catch (e) {128 jQuery.handleError(s, xml, null, e);129 }130 jQuery(# + frameId).load(uploadCallback); //ajax 请求从服务器加载数据,同时传入回调函数131 return { abort: function () { } };132},133uploadHttpData: function (r, type) { var data = !type;134 data = type == "xml" || data ? r.responseXML : r.responseText; // If the type is "script", eval it in global context135 if (type == "script")136 jQuery.globalEval(data); // Get the JavaScript object, if JSON is used.137 if (type == "json")138 eval("data = " + data); // evaluate scripts within html139 if (type == "html")140 jQuery("

").html(data).evalScripts(); return data;141}142 })3。php代码

1 $val){ 40 $files[$i][ ame]=$val; 41 $files[$i][size]=$v[size][$key]; 42 $files[$i][ mp_name]=$v[ mp_name][$key]; 43 $files[$i][error]=$v[error][$key]; 44 $files[$i][ ype]=$v[ ype][$key]; 45 $i++; 46 } 47 } 48} 49return $files; 50 } 51 function uploadFile($path="uploads",$allowExt=array(.gif" alt="JQ插件ajaxFileUpload、php实现图片,数据同时上传 - 云翳1895" title="JQ插件ajaxFileUpload、php实现图片,数据同时上传 - 云翳1895","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true){ 52if(!file_exists($path)){//判断是否有$path文件夹,没有则创建 53 mkdir($path,0777,true);//0777表示最大权限 54} 55$i=0; 56$files=buildInfo(); 57if(!($files&&is_array($files))){ 58 return ; 59} 60foreach($files as $file){ 61 if($file[error]===UPLOAD_ERR_OK){//就是0 62 $ext=getExt($file[ ame]); 63 //检测文件的扩展名 64 if(!in_array($ext,$allowExt)){ 65 exit("非法文件类型"); 66 } 67 //校验是否是一个真正的图片类型 68 if($imgFlag){ 69 if(!getimagesize($file[ mp_name])){ 70exit("不是真正的图片类型"); 7172 }else{ 73$file["filesize"]=getimagesize($file[ mp_name]); 74//把文件信息付给$file 传到前台返回时数组 75//如 [720, 1280, 2, "width="720" height="1280"", 8, 3, "image/jpeg"] 76 } 77 } 78 //上传文件的大小 79 if($file[size]>$maxSize){ 80 exit("上传文件过大"); 81 } 82 if(!is_uploaded_file($file[ mp_name])){ 83 exit("不是通过HTTP POST方式上传上来的"); 84 } 85 $filename=getUniName().".".$ext;//改文件重新命名 86 $destination=$path."/".$filename; 87 if(move_uploaded_file($file[ mp_name], $destination)){ 88 $file[ ame]=$filename; 89 $file[path_name]=$destination; 90 unset($file[ mp_name],$file[size],$file[ ype]);//去除不需要传给的信息 91 $uploadedFiles[$i]=$file; 92 $i++; 93 } 94 }else{ 95 switch($file[error]){ 96case 1: 97 $mes="超过了配置文件上传文件的大小";//UPLOAD_ERR_INI_SIZE 98 break; 99case 2:100 $mes="超过了表单设置上传文件的大小"; //UPLOAD_ERR_FORM_SIZE101 break;102case 3:103 $mes="文件部分被上传";//UPLOAD_ERR_PARTIAL104 break;105case 4:106 $mes="没有文件被上传1111";//UPLOAD_ERR_NO_FILE107 break;108case 6:109 $mes="没有找到临时目录";//UPLOAD_ERR_NO_TMP_DIR110 break;111case 7:112 $mes="文件不可写";//UPLOAD_ERR_CANT_WRITE;113 break;114case 8:115 $mes="由于PHP的扩展程序中断了文件上传";//UPLOAD_ERR_EXTENSION116 break;117 }118 echo $mes;119 }120}121return $uploadedFiles;122 };123 124 $rows=uploadFile($path="uploads",$allowExt=array("gif","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true);125 126 echo json_encode($rows);

uploadFile($path=”uploads”,$allowExt=array(“gif”,”jpeg”,”png”,”jpg”,”wbmp”),$maxSize=2097152,$imgFlag=true);

这段php代码可以更改的可以上传其他文件和大小限制。getimagesize是判断是否为病毒文件更改后缀。

上述代码直接复制即可完成图片和用户数据的同时上传。

返回结果:[{“name”:”d032a4ee7e957d956c8af0039d7e3085.jpg”,”error”:0,”filesiz”:{“0″:720,”1″:1280,”2″:2,”3″:”width=\”720\” height=\”1280\””,”bits”:8,”channels”:3,”mime”:”image\/jpeg”},”path_name”:”uploads\/d032a4ee7e957d956c8af0039d7e3085.jpg”}]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。