1. Preview the file (PDF as an example)
After sharing or sending the file to a friend, the suffix of the file disappeared, and the phone couldn’t be opened. So is this a bug of wechat mini program or my own problem? Emmmm, it must be my own bug. You see other small procedures can be shared…..
// Download and preview the applet image
/ * * * *@param Url Image address *@returns * /
export function conmmonPreviewIOS(url: string) {
const FileSystemManager = wx.getFileSystemManager()
return (
Taro.downloadFile({ / / download first
url: url,
header: {
'content-type': 'application/json',},success: (res) = > {
const Path = res.tempFilePath // Get the temporary file path
let suffixIndex = Path.lastIndexOf('. ');
let suffix = Path.slice(suffixIndex,);
FileSystemManager.saveFile({ // Use the system method to save temporary files
tempFilePath: Path,
filePath: `${wx.env.USER_DATA_PATH}/The ${new Date().getTime()}${suffix}`.// The file name is timestamp
success: (res) = > {
let savedFilePath = res.savedFilePath; // Get the address saved successfully
wx.openDocument({ // Open the document * (when sharing again, the local file name will be shared with the timestamp of the previous step)
filePath: savedFilePath,
showMenu: true.fileType: 'pdf'.success: () = > {
console.log('Document opened successfully'}})}})},fail: (res) = > {
console.log(res, wx.env.USER_DATA_PATH, '0')
wx.showToast({
title: 'Download failed'.duration: 1000})}}))}Copy the code
2. Open external links (Tencent Conference as an example)
As soon as I think of opening the chain, I think of using webView, directly on the method, there is nothing to say, also pure record, next time directly to copy code, so as not to forget….
Click the jump off chain event
const serviceClick = url= > {
Taro.setStorageSync('webWiewUrl', url) // Store the link to be jumped in the storage
commonNav({
url: '/pages/home/courseDetail/components/webView/index'.// Route to webView page
type: 'navigateTo' // Use this to have a back button in the upper left corner to return to our own page})}Copy the code
WebView page code
import React, { Fragment } from 'react'
import { WebView } from '@tarojs/components'
import Taro, { useRouter } from '@tarojs/taro'
const WebViewPages = () = > {
const paramsUrl = useRouter().params.url
let url = paramsUrl || Taro.getStorageSync('webWiewUrl') | |' '
return <Fragment>{url && <WebView src={url} />}</Fragment>
}
export default React.memo(WebViewPages)
Copy the code