收藏
回答

使用wx.uploadFile上传图片,后端返回pdf文件流,前端生成pdf是空白的?

wx.uploadFile({
                url``,
                filePath: res.tempFilePath,
                name'file',
                responseType'arraybuffer',
                header: {
                  'content-type'"multipart/form-data",
                  'accept''application/json',
                  'Authorization''Bearer ' + wx.getStorageSync('userIds').token,
                },
                formData: {},
                success(res) {
                  console.log('res',res)
                  if (res.statusCode === 200) {
                    const fileManager = wx.getFileSystemManager();
                    const FilePath = wx.env.USER_DATA_PATH + "/" + "志愿报告.pdf";
                    fileManager.writeFile({
                      filePath: FilePath,
                      data: res.data,
                      encoding"binary"//编码方式 
                      successres => {
                        wx.openDocument({ //我这里成功之后直接打开
                          filePath: FilePath, 
                          showMenutrue,
                          fileType"pdf",
                          successresult => {
                            wx.hideLoading();
                          },
                          failerr => {
                            wx.hideLoading();
                            wx.showToast({
                              title'打开文档失败!',
                              icon'none'//默认值是success,就算没有icon这个值,就算有其他值最终也显示success
                              duration2000//停留时间
                            })
                          }
                        });
                      },
                      failres => {
                        wx.hideLoading();
                        wx.showToast({
                          title'导出失败!',
                          icon'none'//默认值是success,就算没有icon这个值,就算有其他值最终也显示success
                          duration2000//停留时间
                        })
                      }
                    })
                  } else {
                    wx.hideLoading();
                    wx.showToast({
                      title'生成失败!',
                      icon'none'//默认值是success,就算没有icon这个值,就算有其他值最终也显示success
                      duration2000//停留时间
                    })
                  }


                },
                fail(err) {
                  wx.hideLoading();
                  wx.showToast({
                    title: err,
                    icon'none'//默认值是success,就算没有icon这个值,就算有其他值最终也显示success
                    duration2000//停留时间
                  })
                }
              });
回答关注问题邀请回答
收藏

1 个回答

  • Mr.Zhao
    Mr.Zhao
    12-04

    找你们后端啊

    12-04
    有用
    回复 10
    • 陈佩丽
      陈佩丽
      12-04
      postman可以生成pdf,应该不关后端事
      12-04
      回复
    • Mr.Zhao
      Mr.Zhao
      12-04回复陈佩丽
      写个代码片段出来  能直接运行看结果的
      12-04
      回复
    • 陈佩丽
      陈佩丽
      12-04
      现在只能本地联调,下面是uploadFile返回的结果,能看出什么问题吗?
      12-04
      回复
    • Mr.Zhao
      Mr.Zhao
      12-04回复陈佩丽
      data都乱码了,不是二进制
      12-04
      回复
    • 陈佩丽
      陈佩丽
      12-04
      多谢大佬,我找后端看看
      12-04
      回复
    查看更多(5)
登录 后发表内容