微信小程序> 下载微信图片,微信小程序上传下载本地图片以及微信文件

下载微信图片,微信小程序上传下载本地图片以及微信文件

浏览量:746 时间: 来源:Horizon~
微信小程序上传下载本地图片以及微信文件最近在做一个云盘服务项目,想搭建到微信小程序上,然后讲一下微信小程序中上传和下载的坑
首先申明一下,这个功能真是烦恼了我好久,因为项目中要用到上传word/ppt等文件,找度娘查了半天,对着开发者文档API看了半天,发现根本没有调起手机文件管理器去选择文件上传的API,在微信小程序中是不能调用手机系统的文件管理功能的!!!
但是图片视频这些你已经授权给微信的一些功能是可以调用的,微信也提供了一个api—chooseMessageFile可以获取微信聊天文件
上传本地图片(多张一起上传)wx.chooseImage({success:function(res){console.log(res);count:9;//限制选择数量console.log(res.tempFilePaths)//打印获取到图片的临时地址letsize=0;varnamelist=[];//这是为了给图片命名,用时间命名vardate=newDate();vary=date.getFullYear();varm=date.getMonth()+1;vard=date.getDate();varh=date.getHours();varmin=date.getMinutes();vars=date.getSeconds();//vari=0console.log(y,m,d)//我这边计算了一下文件的大小for(varainres.tempFiles){size+=res.tempFiles[a].sizenamelist.push('m'+y+''+m+''+d+''+h+''+min+''+s+'-'+a+'.jpg')}console.log(namelist)if(size31457280){console.log('上传')//循环上传for(vari=0;ires.tempFilePaths.length;i++){list.push({name:namelist[i],attribute:'file',})vm.setData({list:list})wx.uploadFile({url:app.globalData.url+"/index/upload",filePath:res.tempFilePaths[i],name:'file',//上传的key,和服务器上的一致header:{"Content-Type":"multipart/form-data",'Cookie':app.globalData.cookie},//和wx.request中的data是一样的东西,上传接口参数formData:{'name':namelist[i],'path':vm.data.local_file},success(res){console.log(res)}})}}else{wx.showToast({title:'你选中的图片超过30M!',icon:'none'})}},})上传微信文件wx.chooseMessageFile({count:1,//type:'file',success(res){varfname=res.tempFiles[0].nameconsole.log(res.tempFiles[0])if(res.tempFiles[0].size31457280){wx.uploadFile({url:app.globalData.url+"/index/upload",filePath:res.tempFiles[0].path,name:'file',header:{"Content-Type":"multipart/form-data",'Cookie':app.globalData.cookie},formData:{'name':res.tempFiles[0].name,'path':vm.data.local_file},success(res){console.log(res)list.push({name:fname,attribute:'file',})vm.setData({list:list})}})}else{wx.showToast({title:'你选中的文件超过30M!',icon:'none'})}}})下载文件并打开这里提醒一下,文件下载下来的是临时文件,打开来是可以预览的,如果点转发发给别人的是无法打开的文件。--我没有找到解决方法,试用了savafile,FileSystemManager等API都不能把这个临时文件变成我们要的格式,而且本地文件存储的大小限制为10M,如果有大神知道如何转化,告知我一下非常感谢
wx.downloadFile({url:app.globalData.url+"/index/download?path="+vm.data.local_file+'/'+vm.data.filename,header:{'Content-Type':'application/x-www-form-urlencoded','Cookie':app.globalData.cookie},success(res){if(res.statusCode===200){console.log(res)varfilePath=res.tempFilePath//打开这个文件wx.openDocument({filePath:filePath,success:function(res){console.log('打开文档成功')}})}}})

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎