上传附件
bTapUpFile: function (e) { //上传附件文件 doc, xls, pdf
let that = this;
//var e.currentTarget.id;
wx.chooseMessageFile({
count: 1,
type: 'file',
success(res) {
const tempFilePaths = res.tempFiles;
console.log("临时路径:", tempFilePaths);
wx.uploadFile({
url: getApp().globalData.bUrl + 'wxImg.php',
filePath: tempFilePaths[0].path,
name: 'file',
formData: {
projId: that.data.projId,
projTitle: that.data.projTitle, //项目名称
projItem: e.currentTarget.id,//项目子项过程
upFileN: tempFilePaths[0].name,//上传的文件名
nickName: getApp().globalData.userInfo.nickName,
userName: wx.getStorageSync("username"),
userUnit: wx.getStorageSync("userUnit"),
},
success(res) {
const data = res.data
console.log("data", data);
wx.showToast({
title: '上传成功',
icon: 'success',
duration: 2000
})
}
});
}
})
},
下载文件
bTapDownFile: function (e) {// 下载文件
let that = this;
wx.downloadFile({
url: getApp().globalData.bUrl + 'proImg/'+ that.data.projId+'/'+e.currentTarget.id,
success: function (res) {
var filePath = res.tempFilePath;
console.log(res)
wx.openDocument({
showMenu:true,//这个参数一点要写,不然只能预览文档
filePath: filePath,
success: function (res) {
console.log('打开文档成功')
}
})
}
})
},
服务器端接收
//接收传递参数
$projId = $_POST['projId']; //项目编号
$projDir = 'projImg/'.$projId.'/';//项目文件夹
newDir($projDir);
$projName = getProjName(trim($projId));//项目名称
$noteStr = $_POST["noteStr"];
$nickName = $_POST["nickName"];
$userName = $_POST["userName"];
$userId = empty($userName) ? $nickName : $userName;//注册用户或呢称
$userUnit = $_POST['userUnit']; //用户所在单位
$projItem = $_POST["projItem"];//上传附件
if($projItem){
$tArr = explode("-",$projItem);
$projGep = $tArr[0];
$projSub = $tArr[1];
$kStr = $tArr[2];
upFile($_FILES['file']['tmp_name'],$projDir.$_POST['upFileN']);//上传附件
$wxFile = $_POST['upFileN'];
}else{
$projGep = $_POST["projGep"];
$projSub = $_POST["projSub"];
$kStr = $_POST["kStr"];//子任务,关键字主题
if(strstr($_POST["projSub"],"object")==false){//子过程项
$projSub = $_POST["projSub"];
}else{
$projSub = "";
}
}
if($Longi && $Lant ){//图片有经纬度
$wxFile = "wx".date("YmdHis").".jpg";
$Longi = $_POST['longi'];
$Lant = $_POST['lant'];
//$projGep = $_POST["projGep"]; //项目实施过程
$UpLoc = $_POST['locTxt']; //拍摄地点
upImgFile($_FILES['file']['tmp_name'],$projDir.$wxFile);//上传图片到指定目录
thumbs($projDir.$wxFile,$projDir.$projId.".jpg",200,200);//文件拷贝并缩小
thumbs($projDir.$wxFile,$projDir.$wxFile,560,560);//文件拷贝并缩小
}
ImgRecAdd($wxFile,$UpLoc);//新增项目日常记录
function upFile($tmpName,$upName){//上传文件移到指定目录
if(move_uploaded_file($tmpName, $upName)){
echo "The file ".basename($TempName)." has been uploaded successful.";
}else{
echo "Sorry, there was a problem uploading your file.failed";
}
}