// 下载
dowload:function () {
let t = this
wx.downloadFile({
url: response.data.enclosure_url, // 文件的本身url
filePath: wx.env.USER_DATA_PATH + "/" + response.data.enclosure_title, // 本地自定义的文件名
success: function (res) {
let filePath = res.filePath; // 微信临时文件路径(这里要使用自定义的名字文件名,否则打开的文件名是乱码)
wx.openDocument({
filePath: filePath,
fileType: 'pdf',
showMenu: true, // 是否显示右上角菜单按钮 默认为false(看自身需求,可要可不要。后期涉及到右上角分享功能)
success: function () {
wx.hideLoading()
},
fail: function(e) {
wx.hideLoading()
$.msg(0,'网络繁忙,请稍后重试~')
}
});
},
fail: function() {
wx.hideLoading()
$.msg(0,'网络繁忙,请稍后重试~')
}
});
},
// 上传
uploadFile:function () {
let that = this
wx.chooseMessageFile({
count: 1, //能选择文件的数量
type: 'file', //能选择文件的类型,我这里只允许上传文件.还有视频,图片,或者都可以
success(res) {
var size = res.tempFiles[0].size;
var filename = res.tempFiles[0].name;
var newfilename = filename + "";
console.log(res,1111)
if (size > 5000000||newfilename.indexOf(".pdf")==0){ //我还限制了文件的大小和具体文件类型
wx.showToast({
title: '文件大小不能超过5MB,格式必须为pdf!',
icon: "none",
duration: 2000,
mask: true
})
}else{
$.upload({url:'member/upload/index',file:res.tempFiles[0].path},function(r){
wx.showToast({
title: '上传成功',
icon: "none",
duration: 2000
})
let params = {
name:filename,
file_id:r.data.file_id
}
})
}
}
})
},
封装的$.upload
upload:function(o,f){
var t=this,d={
url:'',
file:'',
name:'file',
data:{}
};
for(var i in d){
if(o[i]) d[i]=o[i]
}
d.data['platform']='miniprogram',
d.data['user-token']=t.visitor.token,
wx.uploadFile({
url:t.siteDomain+'/'+t.siteVersion+'/'+d.url,//上传接口
filePath:d.file,
name:d.name,
formData:d.data,
header:{
'Content-Type':'multipart/form-data',
'accept':'application/json'
},
success(r){
r.data=JSON.parse(r.data),
t.callback(r,f)
},
fail(r){
this.msg(0,'上传失败')
}
})
},