Uploader模块管理网络上传任务,用于从本地上传各种文件到服务器,并支持跨域访问操作。通过plus.uploader可获取上传管理对象。Uploader上传使用HTTP的POST方式提交数据,数据格式符合Multipart/form-data规范,即rfc1867(Form-based File Upload in HTML)协议。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
//为方便理解用数组来形容
var upload= null;
var up_file=null;
//上传图片
function one(wj=null){
upload = plus.uploader.createUpload('http://192.168.31.157/index.php/Login/upfile',{method:'POST'},function(e,status){
if(status==200){
}else{
alert('上传失败'+e);
}
});
if(wj!=null){
upload.addFile(wj,{key:'testdoc'});
}else{
upload.addFile(up_file,{key:'testdoc'});
}
upload.addData('name','学画人生');//用$_post['name']接收的数据
plus.nativeUI.showWaiting("图片上传中");
upload.start();
setTimeout(function(){plus.nativeUI.closeWaiting();alert(upload.responseText);},2000);
}
//从相册选择图片
function getpic(){
plus.gallery.pick(function(e){
for(var i in e.files){
up_file = (e.files[i]);
}
},function(e){
alert(e.message);
document.getElementById("imglist").innerHTML = "";
//设置选择文件的参数
},{filter:"image",maximum:1,multiple:true,system:false});
}
</script>
</head>
<style>
li{line-height: 40px; height: 40px; border-bottom: #ccc dashed 1px;}
</style>
<body>
<ul id="imglist"></ul>
<ul>32
<li onclick="one()">请求上传管理创建新的上传任务</li>
<li onClick="getpic()">获取相册图片</li>
</ul>
</body>
</html>
以下是TP的代码
public function upfile(){
if(@$_FILES["testdoc"]["error"] === 0){
$file = request()->file('testdoc');
$info = $file->rule('uniqid')->move(ROOT_PATH.'public'.DS.'uploads'.DS.'wechatpay');
echo $info->getFilename();
}else{
echo '上传失败';
}
$this->addlogs(date('Y-m-d H:i:s'));
}