当前位置:网站首页>Applet Download Preview PDF, document cannot open solution
Applet Download Preview PDF, document cannot open solution
2022-07-28 23:15:00 【Moon hazy】
The applet opens pdf, Yes 3 Ways of planting
1、webview open , I won't elaborate here
2、 Document binary stream open , Refer to other articles
3、 Today's point ,wx.downloadFile,wx.openDocument
Some pits encountered during operation , First of all, I use the conventional online operation wx.downloadFile,wx.openDocument, The development tool displays normally , The real machine may go to ‘ Failed to open document ’; Real machine debugging found that filePath= wx.env.USER_DATA_PATH + '/' + ‘xxx.pdf’, There is no permission to modify the name if an error is reported , As shown in figure 2 :
Or this bug, After opening the document , Open another pdf file , It is the first time to open pdf file , That is to say, the first cached file is opened , This caching mechanism will be studied by yourself .
if (that.data.pdfSrc1) {
wx.showLoading({
title: 'pdf Preview in preparation ',
})
wx.downloadFile({
url: that.data.pdfSrc1,//pdf link
success(res) {
let filePath= wx.env.USER_DATA_PATH + '/' + ' Report details .pdf';
wx.openDocument({
filePath: filePath,
success: function (res) {
wx.showToast({
title: ' Successfully opened the document ',
})
wx.hideLoading()
},
fail: function (res) {
wx.showToast({
title: ' Failed to open document ',
})
},
})
},
})
}
Solution : Use wx.saveFile Temporary storage path of wechat saved files , Then use the official wx.env.USER_DATA_PATH Modify the local path name , thus , Solve the display problem 、 Cache problem and name modification problem
wx.downloadFile({
url: that.data.pdfSrc1,//pdf link
success(res) {
wx.saveFile({ // a key wx.saveFile Be sure to have Avoid file storage missing Then open failed
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + '/' + ' Report details .pdf',
success(res) {
wx.openDocument({
filePath: res.savedFilePath,
fileType: "pdf",
showMenu: true,
success: function (res) {
wx.showToast({
title: ' Successfully opened the document ',
})
wx.hideLoading()
},
fail: function (res) {
wx.showToast({
title: ' Failed to open document ',
})
},
})
}})
},
})Add a binary stream scheme You need to cooperate with the background to write a parsing interface
wx.request({
url: 'http:/XXXXX/downloadReport.cs?id='+ obj.id,
method: 'POST',
header: {
"content-type": "application/json",
},
responseType: "arraybuffer", // Notice the responseType
success: (result) => {
console.log(" Download successful !", result);
var fileManager = wx.getFileSystemManager();
var FilePath = wx.env.USER_DATA_PATH + '/' + ' Report details 1111'+'.pdf';
// There may also be problems When I open this, it shows that the document is damaged Refer to the above solution
fileManager.writeFile({
data: result.data,
filePath: FilePath,
showMenu: true, // Show upper right menu
encoding: "binary", // Encoding mode
success: res => {
console.log(' Coding format ');
wx.openDocument({ // Open directly after success
filePath: FilePath,
showMenu:true,
fileType: "pdf",
success: result => {
console.log(" Successfully opened the document ");
},
fail: err => {
console.log(" Failed to open document ", err);
}
});
},
fail: res => {
wx.showToast({
title: ' Export failed !',
icon: 'none',
duration: 2000,
})
console.log(res);
}
})
},
fail(err) {
console.log(err)
}
})边栏推荐
- 【MySQL系列】 MySQL表的增删改查(进阶)
- TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to
- Rouyi cloud platform - how to realize the launch and login functions of the project and how to create new modules
- Empowering Chinese core entrepreneurs! See how Moore elite solves the development problems of small and medium-sized chip Enterprises
- Summary of koltin knowledge points
- Seagate released a new risc-v architecture processor: the performance of mechanical hard disk soared 3 times
- This year, MediaTek 5g chip shipments are expected to reach 50million sets!
- (重要)初识C语言 -- 函数
- CGLIb 创建代理
- Thesis reading (0) - alexnet of classification
猜你喜欢
![[C language] implementation of three piece chess games](/img/53/7ee14e604c06fd77d65af29d6d92b8.png)
[C language] implementation of three piece chess games

Date time functions commonly used in MySQL

TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to

Multi activity disaster recovery construction after 713 failure of station B | takintalks share

c语言进阶篇:指针(三)

【MySQL系列】 MySQL表的增删改查(进阶)

The tenth improvement of yolov5: the loss function is improved to Siou

Improvement 11 of yolov5: replace backbone network C3 with lightweight network mobilenetv3

Target detection notes SSD

18张图,直观理解神经网络、流形和拓扑
随机推荐
Introduction to structure
Xinhuazhang announced the completion of more than 200million a-round financing and the comprehensive layout of eda2.0 R & D
Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
[image segmentation] vein segmentation based on directional valley detection with matlab code
can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory
无代码开发平台管理后台入门教程
安全狗入选《云安全全景图2.0》多个细项
Submission records of frontiers Publishing House (with status changes)
The US FCC provided us $1.6 billion to support domestic operators to remove Huawei and ZTE equipment
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
[radar] radar signal online sorting based on kernel clustering with matlab code
ValueError: Using a target size (torch.Size([64])) that is different to the input size (torch.Size([
希捷发布全新RISC-V架构处理器:机械硬盘相关性能暴涨3倍
Es personal arrangement of relevant interview questions
xshell7,xftp7个人免费版官方下载,无需破解,免激活,下载即可使用
c语言进阶篇:指针(三)
《Shortening passengers’ travel time A dynamic metro train scheduling approach using deep reinforcem》
WebView whitelist
Thesis reading (0) - alexnet of classification