初学者随记01:uniapp开发调用微信API,实现在微信小程序上传、下载、打开单个文件功能

一、上传文件
  一、上传文件

ChooseFile() {
               let _this = this
               wx.chooseMessageFile({
                count: 1, //默认100
                extension:                            ['.zip','.doc','.docx','.elsx','.xls','.ppt','.pptx','png','jpg','jpeg'],
                success( res ) {
                    console.log(JSON.stringify(res))
                     _this.fileName = res.tempFiles[0].name // 上传的文件名称
                    if (res.tempFiles.length > 0){
                        uni.uploadFile({
                         url: `${_this.$Url}/receiving/addFile`,//this.$Url 开发中调用后台接口地址
                         filePath: res.tempFiles[0].path, //tempFiles存放选择的文件的本地临时文件对象数组
                         name: "file",
                         formData: {
                          file_name: "file",
                          token:_this.token
                         },
                         success ( res ) {
                          let ret = JSON.parse( res.data )
                          _this.fileSrc = ret.url   //该返回的URL是调后台保存接口要传的路径
                         
                         }
                        })
                    }                
                }
               })
              }

二、下载打开功能
         

   downloadFile(){
                let that = this
                var fileurl = `能打开文件的完整路径`// 看后台返回路径的格式              
                wx.showLoading({
                  title: '加载中',
                })
                setTimeout(function(){
                  wx.hideLoading()
                },2000)

                wx.downloadFile({
                  url: fileurl,
                  success: function (res) {                     
                  const filePath = res.tempFilePath 
                   var filename = fileurl;
                          var index1 = filename.lastIndexOf(".");
                          var index2 = filename.length;
                          var postf = filename.substring(index1, index2);//后缀名
                          var postf1 = postf.replace(/\./g, '')  // 截取的文件后缀,动态赋值打开文件的类型                 
                  console.log("下载成功")
                  if (filePath != null) {                    
                   //文件保存
                   uni.saveFile({                       
                    tempFilePath: filePath,
                    success: function(res) {
                        console.log("保存成功")
                         that.localPath = res.savedFilePath;
                          wx.openDocument({
                                filePath: res.savedFilePath, //这个字段值要赋值保存后的路径,不然文件打不开
                              fileType: postf1,
                                success: function (res) {
                                  console.log('打开文档成功');
                                },
                              fail: function (res) {
                                 console.log(res)
                              }
                          });
                 
                    }
                   });
                
                  }
                 },
                 fail(err){
                  console.log('下载失败', err)
                 }
                });
            },    

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值