Technology Encyclopedia Home >How to implement file upload and download in Ionic?

How to implement file upload and download in Ionic?

To implement file upload and download in Ionic, you can utilize the cordova-plugin-file and cordova-plugin-file-transfer plugins. These plugins provide the necessary functionalities to interact with the device's file system and handle file transfers.

File Upload

For file upload, you typically need to:

  1. Select the File: Use an input element or a file picker to select the file from the device.
  2. Create a FormData Object: This object will hold the file data.
  3. Send the File: Use an HTTP request (like HttpClient in Angular) to send the file to the server.

Example:

import { HttpClient, HttpHeaders } from '@angular/common/http';

constructor(private http: HttpClient) {}

uploadFile(file: File) {
  const formData: FormData = new FormData();
  formData.append('file', file, file.name);

  const headers = new HttpHeaders();
  headers.append('Content-Type', 'multipart/form-data');

  this.http.post('https://your-server.com/upload', formData, { headers: headers })
    .subscribe(response => console.log(response));
}

File Download

For file download, you can:

  1. Send a Request to the Server: Request the file from the server.
  2. Handle the Response: Use the cordova-plugin-file-transfer plugin to save the file to the device's file system.

Example:

import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/file/ngx';

constructor(private transfer: FileTransfer, private file: File) {}

downloadFile() {
  const fileTransfer: FileTransferObject = this.transfer.create();
  const url = 'https://your-server.com/file.pdf';
  const filePath = this.file.dataDirectory + 'file.pdf';

  fileTransfer.download(url, filePath).then((entry) => {
    console.log('download complete: ' + entry.toURL());
  }, (error) => {
    console.log('download error source ' + error.source);
    console.log('download error target ' + error.target);
    console.log('upload error code' + error.code);
  });
}

Cloud Storage Integration

For a more scalable and reliable solution, consider integrating with a cloud storage service like Tencent Cloud's COS (Cloud Object Storage). COS provides a robust and secure way to store and manage files, and it integrates well with Ionic applications.

Tencent Cloud COS Integration:

  1. Create a Bucket: In Tencent Cloud COS, create a bucket to store your files.
  2. Generate Access Credentials: Obtain your SecretId and SecretKey for authentication.
  3. Use COS SDK: Use the COS SDK to upload and download files directly from your Ionic application.

Example with COS SDK:

import { COS } from 'cos-wx-sdk-v5';

const cos = new COS({
  SecretId: 'YOUR_SECRET_ID',
  SecretKey: 'YOUR_SECRET_KEY',
});

cos.putObject({
  Bucket: 'YOUR_BUCKET_NAME-APPID',
  Region: 'YOUR_REGION',
  Key: 'object_name',
  Body: file, // File object from input
}, function(err, data) {
  console.log(err || data);
});

By integrating with Tencent Cloud COS, you can leverage its high availability, scalability, and security features to enhance your Ionic application's file handling capabilities.