微信wx.downloadFile不能下载文件
发布于 6 年前 作者 jie08 4337 次浏览 来自 问答

wx.downloadFile在调用该接口时候,文档名因为是中文名,直接调用失败,后来我进行了

url = encodeURI(url)转码,有些文件可以下载,有得文件就下载不了,连onProgressUpdate

进度函数都没有调用,而是success函数里返回给我一串以.msword结尾的res.tempFilePath数据,{tempFilePath: “http://tmp/wx40161b6ba4238139.o6zAJs1p3tFQnWMBRMqx…dutsbMXDPU1be843e24cdeed91adee4ba80a4695d7.msword”, statusCode: 200, errMsg: “downloadFile:ok”};

但是wx.openDocument是不能识别这串res.tempFilePath;我想主要的原因还是没有下载,因为连onProgressUpdate都没有进入,证明这个文件就没有开始下载,我想问问技术人员这是什么的问题,因为是给政务大厅写的东西,相关文档都要求下载的,忘大佬帮忙解决

2 回复

tempFile就是临时文件的储存路径,这个是已经下下来了,这两天碰到你一样的问题,这个是word的doc格式下载下来后缀名就会改成msword,如果是docx就可以,这一点我也没有解决…如果是存到tempFile,最好wx.downloadFile下面再加一个预览文件的方法wx.openDocument,直接预览,要不然就保存到本地,用wx.saveFile做

download: function(e) {

    var that = this;

    console.log(e)

    var data = {

      “fileName”: e.currentTarget.dataset.name,

      “fileNo”: e.currentTarget.id

    }

    console.log(data)

    wx.request({

      url: getApp().globalData.url + ‘file/download’,

      header: {

        ‘Content-Type’: getApp().globalData.contentType

      },

      method: “POST”,

      data: data,

      dataType: ‘json’,

      success: function(data) {

        console.log(data);

        if (data.data.code == 200) {

          if (e.currentTarget.dataset.name) {

            var url = getApp().globalData.url + “resources/” + e.currentTarget.dataset.name;

            url = encodeURI(url);

            const downloadTask = wx.downloadFile({

              url: url,

              success: function(res) {

                console.info(res)

                if (res.statusCode == 200 && res.tempFilePath) {

                  if (check.check.isImage(res.tempFilePath)) {

                    console.log(“这是张图片”);

                    that.setData({

                      downimg: res.tempFilePath,

                      imgshow: true,

                    })

                  } else {

                    var filePath = res.tempFilePath;

                    wx.saveFile({

                      tempFilePath: filePath,

                      success: function(res1) {

                        console.info(res1)

                        var filep = res1.savedFilePath;

                        wx.openDocument({

                          filePath: filep,

                          success: function(res2) {

                            //console.info(res2)

                            //console.log(‘打开文档成功’)

                          },

                          fail: function(res) {

                            //console.log(res)

                            wx.showToast({

                              title: ‘该文件暂不提供下载!’,

                              icon: ‘none’

                            });

                          }

                        })

                      }

                    })

                  }

                }

              }

            })

            downloadTask.onProgressUpdate((res) => {

              wx.showLoading({

                title: '下载进度 ’ + res.progress + “%”,

              })

              if (res.progress == 100){

                wx.hideLoading();

              }

            })

          } else {

            wx.showToast({

              title: ‘该文件暂不提供下载!’,

              icon: ‘none’

            });

          }

        } else {

          wx.showToast({

            title: ‘该文件暂不提供下载!’,

            icon: ‘none’

          });

        }

      }

    })

这是我dwonload的整个代码

回到顶部