File APIs

openDocument

Preview a PDF file within the mini program.

Overview

my.openDocument 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

my.downloadFile({
  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:

PropertyTypeRequiredDescription
filePathStringYesObtain file path via my.downloadFile.
fileTypeStringYesFile type.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).
ValueDescription
pdfPDF format.

Error code

Error codeDescriptionSolution
4011File path is invalid or no permission to access incoming path.Check the incoming file path.
4012Preview files do not exist.Make sure files that correspond to file path exist.
4013File format is not supported.Currently preview of PDF files is supported.

Usage Notes

  • Currently only PDF file format is supported
  • File path must be obtained via my.downloadFile
  • Preview opens within the mini program without navigation away
  • User returns to the same page after closing preview