当前位置:网站首页>Wechat applet part of the mobile phone Preview PDF did not respond

Wechat applet part of the mobile phone Preview PDF did not respond

2022-04-23 19:08:00 Gougouliang

Today, my colleagues reported that , preview PDF Cannot be opened . Other colleagues can open .
After debugging with him, I found , Indicates that the cache is full .
 Insert picture description here
adopt api FileSystemManager.readdir
Show him the cached files , Found to have 234 individual .
 Insert picture description here
Let it clean up the cache , Preview normally PDF 了 .
Post the code

// preview PDF
previewPdf(params) {
    
		uni.showLoading({
    
			title: 'loading'
		})
		GetPhoto(params.filePath).then(res => {
    
			uni.hideLoading()
			if (res.statusCode == 400) {
    
				this.$Toast(' file does not exist ')
				return
			}
			let pdfBase64 = res.data
			let fs = uni.getFileSystemManager();
			fs.writeFile({
    
				filePath: wx.env.USER_DATA_PATH + "/" + params.fileName + '.pdf',
				data: uni.base64ToArrayBuffer(pdfBase64.replace(/[\r\n]/g, "")),
				success: res => {
    
					uni.openDocument({
    
						filePath: wx.env.USER_DATA_PATH + "/" + params.fileName +
							'.pdf',
						showMenu: true,
						success: function(res) {
    
						},
						fail(err) {
    
						}
					})
				}
			})
		})
	},
// Clean cache 
removeFile() {
    
		uni.showLoading({
    
			title:' Cleaning up ...'
		})
		const fs = wx.getFileSystemManager()
		fs.readdir({
    
			dirPath: `${
    wx.env.USER_DATA_PATH}/`,
			success(res) {
    
				let length = 0
				for (let i of res.files) {
    
					length++
					if (length == res.files.length) {
    
						setTimeout(()=>{
    
							uni.hideLoading()
							uni.showToast({
    
								title: ' Clean up '
							})
						},500)
					}
					let name = (`${
    wx.env.USER_DATA_PATH}/` + i)
					if (name.indexOf('miniprogramLog') > -1) {
      // No permission to delete   skip 
						continue;
					}
					fs.unlink({
    
						filePath: name,
						fail(e) {
    
							console.log(' File deletion failed :', e)
						},
						success(succ) {
    
							console.log(' File deleted successfully :', succ);
						}
					})
				}
			},
			fail(res) {
    
				console.error(res)
			}
		})
	},

版权声明
本文为[Gougouliang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210602029749.html