@ionic-native/file-transfer

  • Version 5.36.0
  • Published
  • 50.2 kB
  • 1 dependency
  • MIT license

Install

npm i @ionic-native/file-transfer
yarn add @ionic-native/file-transfer
pnpm add @ionic-native/file-transfer

Overview

Ionic Native - Native plugins for ionic apps

Index

Variables

variable FileTransfer

const FileTransfer: FileTransferOriginal;

    Classes

    class FileTransferObject

    class FileTransferObject {}

    constructor

    constructor();

      method abort

      abort: () => void;
      • Aborts an in-progress transfer. The onerror callback is passed a FileTransferError object which has an error code of FileTransferError.ABORT_ERR.

      method download

      download: (
      source: string,
      target: string,
      trustAllHosts?: boolean,
      options?: { [s: string]: any }
      ) => Promise<any>;
      • Downloads a file from server.

        Parameter source

        URL of the server to download the file, as encoded by encodeURI().

        Parameter target

        Filesystem url representing the file on the device. For backwards compatibility, this can also be the full path of the file on the device.

        Parameter trustAllHosts

        Optional parameter, defaults to false. If set to true, it accepts all security certificates. This is useful because Android rejects self-signed security certificates. Not recommended for production use. Supported on Android and iOS.

        Parameter Optional

        parameters, currently only supports headers (such as Authorization (Basic Authentication), etc).

        Returns

        {Promise} Returns a Promise that resolves to a FileEntry object.

      method onProgress

      onProgress: (listener: (event: ProgressEvent) => any) => void;
      • Registers a listener that gets called whenever a new chunk of data is transferred.

        Parameter listener

        Listener that takes a progress event.

      method upload

      upload: (
      fileUrl: string,
      url: string,
      options?: FileUploadOptions,
      trustAllHosts?: boolean
      ) => Promise<FileUploadResult>;
      • Sends a file to a server.

        Parameter fileUrl

        Filesystem URL representing the file on the device or a data URI. For backwards compatibility, this can also be the full path of the file on the device.

        Parameter url

        URL of the server to receive the file, as encoded by encodeURI().

        Parameter options

        Optional parameters.

        Parameter trustAllHosts

        Optional parameter, defaults to false. If set to true, it accepts all security certificates. This is useful since Android rejects self-signed security certificates. Not recommended for production use. Supported on Android and iOS.

        Returns

        {Promise} Returns a Promise that resolves to a FileUploadResult and rejects with FileTransferError.

      class FileTransferOriginal

      class FileTransferOriginal extends IonicNativePlugin {}
      • File Transfer

        This plugin allows you to upload and download files.

        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

        FileUploadOptions FileUploadResult FileTransferError FileTransferObject

      property FileTransferErrorCode

      FileTransferErrorCode: {
      FILE_NOT_FOUND_ERR: number;
      INVALID_URL_ERR: number;
      CONNECTION_ERR: number;
      ABORT_ERR: number;
      NOT_MODIFIED_ERR: number;
      };
      • 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 {number}

      method create

      create: () => FileTransferObject;
      • Creates a new FileTransfer object {FileTransferObject}

      Interfaces

      interface FileTransferError

      interface FileTransferError {}

        property body

        body: string;
        • Response body. This attribute is only available when a response is received from the HTTP connection.

        property code

        code: number;
        • One of the predefined error codes listed below.

        property exception

        exception: string;
        • Either e.getMessage or e.toString.

        property http_status

        http_status: number;
        • HTTP status code. This attribute is only available when a response code is received from the HTTP connection.

        property source

        source: string;
        • URL to the source.

        property target

        target: string;
        • URL to the target.

        interface FileUploadOptions

        interface FileUploadOptions {}

          property chunkedMode

          chunkedMode?: boolean;
          • Whether to upload the data in chunked streaming mode. Defaults to true.

          property fileKey

          fileKey?: string;
          • The name of the form element. Defaults to 'file'.

          property fileName

          fileName?: string;
          • The file name to use when saving the file on the server. Defaults to 'image.jpg'.

          property headers

          headers?: {
          [s: string]: any;
          };
          • 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.

          property httpMethod

          httpMethod?: string;
          • The HTTP method to use - either PUT or POST. Defaults to POST.

          property mimeType

          mimeType?: string;
          • The mime type of the data to upload. Defaults to image/jpeg.

          property params

          params?: {
          [s: string]: any;
          };
          • A set of optional key/value pairs to pass in the HTTP request.

          interface FileUploadResult

          interface FileUploadResult {}

            property bytesSent

            bytesSent: number;
            • The number of bytes sent to the server as part of the upload.

            property headers

            headers: {
            [s: string]: any;
            };
            • The HTTP response headers by the server.

            property response

            response: string;
            • The HTTP response returned by the server.

            property responseCode

            responseCode: number;
            • The HTTP response code returned by the server.

            Package Files (1)

            Dependencies (1)

            Dev Dependencies (0)

            No dev dependencies.

            Peer Dependencies (2)

            Badge

            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@ionic-native/file-transfer.

            • Markdown
              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@ionic-native/file-transfer)
            • HTML
              <a href="https://www.jsdocs.io/package/@ionic-native/file-transfer"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>