my.openDocument
The my.openDocument API enables users to preview a PDF file within the mini program.
After clicking on the PDF file, the user can preview the file without leaving the mini program. After closing the preview, the user will still be at the current mini program page.
Sample code
.js
copy
// .js
my.downloadFile({
// Sample url that is not real
url: 'http://documentExample.com/alipay.pdf',
success({ apFilePath }) {
my.hideLoading();
my.openDocument({
filePath: apFilePath,
fileType: 'pdf',
success: (res) => {
console.log('open document success')
}
})
}
})
Parameters
Object type
Property | Type | Required | Description |
filePath | String | Yes | Obtain file path via my.downloadFile. |
fileType | String | Yes | File type. |
success | Function | No | Callback function upon call success. |
fail | Function | No | Callback function upon call failure. |
complete | Function | No | Callback function upon call completion (to be executed upon either call success or failure). |
Legal values for fileType
Value | Description |
PDF format. |
Error code
Error code | Description | Solution |
4011 | File path is invalid or no permission to access incoming path. | Check the incoming file path. |
4012 | Preview files do not exist. | Make sure files that correspond to file path exist. |
4013 | File format is not supported. | Currently preview of PDF files is supported. |