Framework v4 BETA

FileTransfer

This plugin allows you to upload and download files.

Repo: https://github.com/apache/cordova-plugin-file-transfer

Installation

  1. Install the Cordova and Ionic Native plugins:
    shell
    $ ionic cordova plugin add cordova-plugin-file-transfer
    $ npm install --save @ionic-native/[email protected]
    
  2. Add this plugin to your app's module

Supported Platforms

  • Amazon Fire OS
  • Android
  • Browser
  • iOS
  • Ubuntu
  • Windows
  • Windows Phone

Usage

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

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

...

const fileTransfer: FileTransferObject = this.transfer.create();

// Upload a file:
fileTransfer.upload(..).then(..).catch(..);

// Download a file:
fileTransfer.download(..).then(..).catch(..);

// Abort active transfer:
fileTransfer.abort();

// full example
upload() {
  let options: FileUploadOptions = {
     fileKey: 'file',
     fileName: 'name.jpg',
     headers: {}
     .....
  }

  fileTransfer.upload('<file path>', '<api endpoint>', options)
   .then((data) => {
     // success
   }, (err) => {
     // error
   })
}

download() {
  const url = 'http://www.example.com/file.pdf';
  fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
    console.log('download complete: ' + entry.toURL());
  }, (error) => {
    // handle error
  });
}

To store files in a different/publicly accessible directory, please refer to the following link https://github.com/apache/cordova-plugin-file#where-to-store-files


Instance Members

FileTransferErrorCode

Error code rejected from upload with FileTransferError Defined in FileTransferError. FILE_NOT_FOUND_ERR: 1 Return when file was not found INVALID_URL_ERR: 2, Return when url was invalid CONNECTION_ERR: 3, Return on connection error ABORT_ERR: 4, Return on aborting NOT_MODIFIED_ERR: 5 Return on '304 Not Modified' HTTP response


create

Creates a new FileTransfer object


FileTransferError

body

Type: intrinsic
Response body. This attribute is only available when a response is received from the HTTP connection.

code

Type: intrinsic
One of the predefined error codes listed below.

exception

Type: intrinsic
Either e.getMessage or e.toString.

http_status

Type: intrinsic
HTTP status code. This attribute is only available when a response code is received from the HTTP connection.

source

Type: intrinsic
URL to the source.

target

Type: intrinsic
URL to the target.

FileUploadOptions

chunkedMode

Type: intrinsic
Whether to upload the data in chunked streaming mode. Defaults to true. optional

fileKey

Type: intrinsic
The name of the form element. Defaults to 'file'. optional

fileName

Type: intrinsic
The file name to use when saving the file on the server. Defaults to 'image.jpg'. optional

headers

Type: reflection
A map of header name/header values. Use an array to specify more than one value. On iOS, FireOS, and Android, if a header named Content-Type is present, multipart form data will NOT be used. optional

httpMethod

Type: intrinsic
The HTTP method to use - either PUT or POST. Defaults to POST. optional

mimeType

Type: intrinsic
The mime type of the data to upload. Defaults to image/jpeg. optional

params

Type: reflection
A set of optional key/value pairs to pass in the HTTP request. optional

FileUploadResult

bytesSent

Type: intrinsic
The number of bytes sent to the server as part of the upload.

headers

Type: reflection
The HTTP response headers by the server.

response

Type: intrinsic
The HTTP response returned by the server.

responseCode

Type: intrinsic
The HTTP response code returned by the server.