收藏
回答

原生微信小程序,安卓机型打开pdf失败,ios可以正常打开?

wx.downloadFile({
        url: fileUrl,
        success(res) {
          console.log(res, '下载成功');
          const filePath = res.tempFilePath;
          wx.openDocument({
            filePath: filePath,
            fileType'pdf',
            showMenutrue,
            successfunction (res{
              wx.showToast({
                title'打开文档成功',
                icon'none'
              });
            },
            failfunction (res{
              wx.showToast({
                title'打开文档失败',
                icon'none'
              });
              console.error('打开文档失败', res);
            }
          });
        },
        fail(res) {
          wx.showToast({
            title'下载文件失败',
            icon'none'
          });
          console.error('下载文件失败', res);
        }
      });
回答关注问题邀请回答
收藏

2 个回答

  • Y.
    Y.
    04-17

    downloadFile要加filePath(指定文件下载后存储的路径),你再试试

    const fileName = fileUrl.split('/').pop();//截取文件名+后缀
    wx.downloadFile({
    	url: fileUrl,
    	filePath: wx.env.USER_DATA_PATH + '/' + fileName,//指定文件下载后存储的路径 (本地路径)
    	success(res) {
    	  console.log(res, '下载成功');
    	  const filePath = res.filePath;
    	  wx.openDocument({
    		filePath: filePath,
    		fileType: 'pdf',
    		showMenu: true,
    		success: function (res) {
    		  wx.showToast({
    			title: '打开文档成功',
    			icon: 'none'
    		  });
    		},
    		fail: function (res) {
    		  wx.showToast({
    			title: '打开文档失败',
    			icon: 'none'
    		  });
    		  console.error('打开文档失败', res);
    		}
    	  });
    	},
    	fail(res) {
    	  wx.showToast({
    		title: '下载文件失败',
    		icon: 'none'
    	  });
    	  console.error('下载文件失败', res);
    	}
      });
    


    04-17
    有用
    回复 1
    • Y.
      Y.
      04-17
      还有 const filePath = res.tempFilePath; 要改成 const filePath = res.filePath;
      04-17
      回复
  • 智能回答 智能回答 该问答由AI生成
    04-17
    有用
登录 后发表内容