Skip to content

Cordova Plugin to preview any file in native mode by providing the local or external URL

License

Notifications You must be signed in to change notification settings

beard7/previewAnyFile

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Preview Any File

Whatever the file is PDF document, Word document, Excel, office document, archive file, image, text, html or anything else, you can perform a preview by this awesome cordova Plugin to preview any file in native mode by providing the local or external URL.

From version 0.2.0, you can preview the file from any where, you can use base64 or local file from assets or local files from the device or even from internet.

From version 0.2.0, you can overwrite the file name or its mime type, if the file has no extension at the end of the path

you can find the project at github

https://github.com/mobiliarmad/previewAnyFile

How it work

For IOS

Using the built-in class QLPreviewController, A QLPreviewController can display previews for the following items:

  • iWork documents
  • Microsoft Office documents (Office ‘97 and newer)
  • Rich Text Format (RTF) documents
  • PDF files
  • Images
  • Text files whose uniform type identifier (UTI) conforms to the public.text type (see Uniform Type Identifiers Reference)
  • Comma-separated value (csv) files
  • 3D models in USDZ format (with both standalone and AR views for viewing the model)

for any other file it will present the preview with message "cannot preview file" and there is a button to save the file in the device

For Android

Android not like ios, there is no way to render the file directly, the user must install a suitable application for every type.

So I used Intent.ACTION_VIEW to preview the file,

if there is a suitable application already installed in the user device it will preview the file directly,

if not, it will ask the user to select an application

the file path in android must be absolute path to the file starting with file:// or content://

Install

$ cordova plugin add @capacitor-mobi/cordova-plugin-preview-any-file --save

for ionic projects

$ ionic cordova plugin add @capacitor-mobi/cordova-plugin-preview-any-file --save

Usage

Use this code to preview the file,

if the correct extension not exist at the last of the file path like file://filepath/filename.ext you must define the file name or its mimeType in the 2nd params {name:'file.png',mimeType : 'image/png'}

- View or Open file from the device

    window.PreviewAnyFile.previewPath(
        win =>
            {
                if (win == "SUCCESS") {
                    console.log('success')
                } else if (win == "CLOSING") {
                    console.log('closing')
                } else if (win == "NO_APP") {
                    console.log('no suitable app to open the file (mainly will appear on android')
                } else {
                    console.log('error')
                }
            },
        error => console.error("open failed", error),
        "file://filepath/filename.ext"
    );

- View or Open file from an external link

you can use external link, the preview will not opened until the file downloaded successfully, so present loader before call the function then dismiss it in call back, you can define the name of the file or its mimeType in the options