ThinkPHP 使用上传模块(ajaxFileUpload)
下载Uploadfile类文件
http://www.thinkphp.cn/extend/224.html
放到:
ThinkPHP/Extend/Library/ORG/Net 。
修改文件头部,加上namespace:<?php<br />
namespace OrgNet;
namespace OrgNet;官方文档位置:
http://doc.thinkphp.cn/manual/upload.html
$.ajaxFileUpload({<br />
url: _app_ '/Products/Items/upload',<br />
secureuri: false,<br />
fileElementId: 'uploadId',<br />
dataType: 'json',<br />
data:$("form[name=fmAdd]").serializeArray(),<br />
success: function (data, status) {<br />
var data_obj = JSON.parse(data);<br />
console.log(data_obj);<br />
},<br />
error: function (data, status, e) {<br />
console.log('error');<br />
return;<br />
} <br />
});
javascript代码
$.ajaxFileUpload({public function upload(){<br />
if(!isset($this->U)){<br>
return array('result'=>'Timeout');<br>
}<br>
// import('Org.Net.UploadFile');<br>
$upload = new OrgNetUploadFile();<br>
//设置上传文件大小<br>
//$upload->maxSize = 3292200;<br>
//设置上传文件类型<br>
$upload->allowExts = explode(',', 'txt,csv');<br>
//设置附件上传目录<br>
$upload->savePath = './Uploads/';<br>
if (!$upload->upload()) {<br>
//捕获上传异常<br>
//$this->error($upload->getErrorMsg());<br>
$this->response(array("result"=>"Fail"),'json');<br>
} else {<br>
//取得成功上传的文件信息<br>
$uploadList = $upload->getUploadFileInfo();<br>
$savename = $uploadList[0]['savename'];<br>
$this->response(array("result"=>"Success","url"=>$savename ),'json');<br>
} <br>
}
url: _app_ '/Products/Items/upload',
secureuri: false,<br>
fileElementId: 'uploadId',<br>
dataType: 'json',
data:$("form[name=fmAdd]").serializeArray(),
success: function (data, status) {
var data_obj = JSON.parse(data);
console.log(data_obj);
},
error: function (data, status, e) {
console.log('error');
return;
}
});
PHP代码public function upload(){
if(!isset($this->U)){
return array('result'=>'Timeout');
}
// import('Org.Net.UploadFile');
$upload = new OrgNetUploadFile();
//设置上传文件大小
//$upload->maxSize = 3292200;
//设置上传文件类型
$upload->allowExts = explode(',', 'txt,csv');
//设置附件上传目录
$upload->savePath = './Uploads/';
if (!$upload->upload()) {
//捕获上传异常
//$this->error($upload->getErrorMsg());
$this->response(array("result"=>"Fail"),'json');
} else {
//取得成功上传的文件信息
$uploadList = $upload->getUploadFileInfo();
$savename = $uploadList[0]['savename'];
$this->response(array("result"=>"Success","url"=>$savename ),'json');
}
}html代码
AD:真正免费,域名 虚机 企业邮箱=0元