Overview
Ionic is an open-source framework and the most popular cross-platform mobile app framework. It is helpful for developing or building hybrid mobile apps quickly and easily. It will save a lot of time and money for investment.
This article demonstrates how to download a file in ionic 3 using a native file transfer plugin integrated with the SharePoint Framework. The following tools and requirements should be ready before starting this article.
Once you've installed Node.js we can install other tools on the terminal or command line. Run the following commands step by step.
npm install -g ionic cordova
Installation is finished, so let’s start creating an Ionic 3 app.
Create an Ionic 3 app
Open your node.js terminal and open a specific location to create the Ionic 3 app.
Start a new Ionic project using ionic start command,
ionic start myApp
This command will take a few minutes because it installs all dependencies and modules for a project.
Now, change the location to the myApp.
cd myApp
Once you've changed the location to myApp, Add the android platform to our app.
- Ionic cordova platform -- add Android
- Ionic cordova platform -- add browser
Now run a project using the following command.
ionic serve (For Browser)
ionic cordova run android (For Device)
Make sure you have to connected devices and settings have been set.
Now let’s start.
File Transfer
The File Transfer plugin allows you to upload a file and download a file.
File
Using the File Plugin we can achieve the following:
Get available free space, check if a directory is available or not, create a directory, remove a directory, move a directory one place to another place, copy a directory, list an available directory, set a pre-defined path, check if a file is available or not, create a file, remove a file, write a file, read a file as text, read a file and return base64 data, read a file and return data as a binary format, read a file and return data as array buffer format, move a file, copy a file, get a directory, get a file, and more.
Install and configure File Transfer and File
Follow the below-mentioned steps.
Step1
Now we have our Ionic app so we can move forward. First, install a File Transfer plugin to our ionic app using the following steps.
ionic cordova plugin add cordova-plugin-file-transfer
npm install --save @ionic-native/file-transfer
Now install the file plugin to our ionic app using the following steps.
ionic cordova plugin add cordova-plugin-file
npm install --save @ionic-native/file
Step 2
Now file transfer plugin is already installed so we need to add File Transfer plugin to our app module file.
app.module.ts file
-
- import {
- FileTransfer,
- FileTransferObject
- } from '@ionic-native/file-transfer';
- import {
- File
- } from '@ionic-native/file';
- @NgModule({
- providers: [
-
- FileTransfer,
- FileTransferObject,
- File
- ]
- })
- export class AppModule {}
Step3
Now it’s time to integrate with component part in our app.
app.ts file
- import {
- FileTransfer,
- FileTransferObject
- } from '@ionic-native/file-transfer';
- import {
- File
- } from '@ionic-native/file';
-
- private fileTransfer: FileTransferObject;
-
- constructor(private transfer: FileTransfer, private file: File) {}
- public download(fileName, filePath) {
-
- let url = encodeURI(filePath);
-
- this.fileTransfer = this.transfer.create();
-
- fileTransfer.download(url, this.file.externalRootDirectory + fileName, true).then((entry) => {
-
- console.log('download completed: ' + entry.toURL());
- }, (error) => {
-
- console.log('download failed: ' + error);
- });
- }
In fileTransfer.download method, I am passing three arguments
- source location, (online file location url)
- target location, (path to save in mobile)
- trustAllHosts, (optional, defaults to false. If set to true, it accepts all security certificates.)
- Options, (Not using now this method. currently only supports headers (such as Authorization (Basic Authentication), etc).)
Step 4
Calling the download method and passing the arguments:
- this.download("sample.pdf","https://abcd.sharepoint.com/samplesite/Shared Documents/sample.pdf");
For further details visit the official website
File Transfer
https://ionicframework.com/docs/native/file-transfer/#FileTransferObject
File
https://ionicframework.com/docs/native/file/
In this article, I discussed how to download a file using the native file transfer plugin and SharePoint Framework (SPFx) integrated with ionic 3. And I tested pdf files and Excel files only.
If you have any questions/issues about this article, please let me know in the comments.