扫描小程序码分享
7 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
代码如下:
export function downAttachment(url) { //加载框动画 uni.showLoading({ title: '正在下载……' }); uni.downloadFile({ url, success: (data) => { if (data.statusCode === 200) { //隐藏加载框 uni.hideLoading(); //文件保存到本地 uni.saveFile({ tempFilePath: data.tempFilePath, //临时路径 success: function(res) { // console.log('打印res',res) uni.showToast({ icon: 'success', mask: true, // title: '文件已保存:' + res.savedFilePath, //保存路径 title: '下载成功', duration: 2000, }); //自动打开文档查看 setTimeout(() => { const filePath = res.savedFilePath; uni.openDocument({ //新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。 filePath: filePath, showMenu: true, success: function(res) { console.log('打开文档成功'); } }); }, 2000) } }); } }, fail: (err) => { uni.showToast({ icon: 'none', mask: true, title: '文件下载失败', }); }, }); }
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
请问解决没有呢?同样的问题,空白,页码正确
后端返回的二进制数据,返回前用 base64 编码一下,小程序那里直接用 base64 去解析就可以
const fs = wx.getFileSystemManager()
fs.writeFile({
filePath:"地址",
data: 文件流( 在这里怎么转 base64),
encoding:"binary",
success(){
wx.openDocument({
// 文件打开
}
})
...
现在这个版本微信浏览器打开pdf链接 统一都有这个问题的。
搞个代码片段
let prams = {
docCode:e.currentTarget.dataset.item.docCode,
annexCode:e.currentTarget.dataset.item.annexCode,
download:true
let newPath = wx.env.USER_DATA_PATH+ '/' + e.currentTarget.dataset.item.annexName
let that = this
app.globalData.Request.postRequest("doc-manage/docInfo/download?docCode=" + e.currentTarget.dataset.item.docCode + '&annexCode=' + e.currentTarget.dataset.item.annexCode, prams,
function (res) {
// 这个res就是后端给的二进制的pdf流文件,我不知道是不是我哪里写的不对
const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
filePath: newPath, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
data: res,
encoding: "binary", //二进制流文件必须是 binary
fail(err) {
console.log("调用失败",err)
},
success (res){
console.log("获取的地址",newPath)
wx.openDocument({ // 打开文档
filePath:newPath, //拿上面存入的文件路径
fileType:'pdf',
fail(ree) {
console.log("这个失败是什么",ree)
success: function (res) {
setTimeout(()=>{wx.hideLoading()},500)
function (err) {
console.log("err", err)
下载 然后 打开
wx.downloadFile({ // 示例 url,并非真实存在 url: 'http://example.com/somefile.pdf', success: function (res) { const filePath = res.tempFilePath wx.openDocument({ filePath: filePath, success: function (res) { console.log('打开文档成功') } }) } })
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
代码如下:
export function downAttachment(url) { //加载框动画 uni.showLoading({ title: '正在下载……' }); uni.downloadFile({ url, success: (data) => { if (data.statusCode === 200) { //隐藏加载框 uni.hideLoading(); //文件保存到本地 uni.saveFile({ tempFilePath: data.tempFilePath, //临时路径 success: function(res) { // console.log('打印res',res) uni.showToast({ icon: 'success', mask: true, // title: '文件已保存:' + res.savedFilePath, //保存路径 title: '下载成功', duration: 2000, }); //自动打开文档查看 setTimeout(() => { const filePath = res.savedFilePath; uni.openDocument({ //新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。 filePath: filePath, showMenu: true, success: function(res) { console.log('打开文档成功'); } }); }, 2000) } }); } }, fail: (err) => { uni.showToast({ icon: 'none', mask: true, title: '文件下载失败', }); }, }); }请问解决没有呢?同样的问题,空白,页码正确
后端返回的二进制数据,返回前用 base64 编码一下,小程序那里直接用 base64 去解析就可以
const fs = wx.getFileSystemManager()fs.writeFile({filePath:"地址",data: 文件流( 在这里怎么转 base64),encoding:"binary",success(){wx.openDocument({filePath:"地址",success(){// 文件打开}})}})...
现在这个版本微信浏览器打开pdf链接 统一都有这个问题的。
搞个代码片段
let prams = {docCode:e.currentTarget.dataset.item.docCode,annexCode:e.currentTarget.dataset.item.annexCode,download:true}let newPath = wx.env.USER_DATA_PATH+ '/' + e.currentTarget.dataset.item.annexNamelet that = thisapp.globalData.Request.postRequest("doc-manage/docInfo/download?docCode=" + e.currentTarget.dataset.item.docCode + '&annexCode=' + e.currentTarget.dataset.item.annexCode, prams,function (res) {// 这个res就是后端给的二进制的pdf流文件,我不知道是不是我哪里写的不对const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器fs.writeFile({filePath: newPath, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义data: res,encoding: "binary", //二进制流文件必须是 binaryfail(err) {console.log("调用失败",err)},success (res){console.log("获取的地址",newPath)wx.openDocument({ // 打开文档filePath:newPath, //拿上面存入的文件路径fileType:'pdf',fail(ree) {console.log("这个失败是什么",ree)},success: function (res) {setTimeout(()=>{wx.hideLoading()},500)}})},})},function (err) {console.log("err", err)})下载 然后 打开
wx.downloadFile({ // 示例 url,并非真实存在 url: 'http://example.com/somefile.pdf', success: function (res) { const filePath = res.tempFilePath wx.openDocument({ filePath: filePath, success: function (res) { console.log('打开文档成功') } }) } })let newPath = wx.env.USER_DATA_PATH+ '/' + e.currentTarget.dataset.item.annexName
let that = this
app.globalData.Request.postRequest("doc-manage/docInfo/download?docCode=" + e.currentTarget.dataset.item.docCode + '&annexCode=' + e.currentTarget.dataset.item.annexCode, prams,
function (res) {
const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
fs.writeFile({
filePath: newPath, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
data: res,
// encoding: "application/json;charset=UTF-8", //二进制流文件必须是 binary
// encoding: "application/json", //二进制流文件必须是 binary
encoding: "binary", //二进制流文件必须是 binary
fail(err) {
console.log("调用失败",err)
},
success (res){
console.log("获取的地址",newPath)
wx.openDocument({ // 打开文档
filePath:newPath, //拿上面存入的文件路径
fileType:'pdf',
fail(ree) {
console.log("这个失败是什么",ree)
},
success: function (res) {
setTimeout(()=>{wx.hideLoading()},500)
}
})
},
})
},
function (err) {
console.log("err", err)
})