UNPKG

7 kBTypeScriptView Raw
1export interface FileUploadOptions {
2 /**
3 * The name of the form element.
4 * Defaults to 'file'.
5 */
6 fileKey?: string;
7 /**
8 * The file name to use when saving the file on the server.
9 * Defaults to 'image.jpg'.
10 */
11 fileName?: string;
12 /**
13 * The HTTP method to use - either PUT or POST.
14 * Defaults to POST.
15 */
16 httpMethod?: string;
17 /**
18 * The mime type of the data to upload.
19 * Defaults to image/jpeg.
20 */
21 mimeType?: string;
22 /**
23 * A set of optional key/value pairs to pass in the HTTP request.
24 */
25 params?: {
26 [s: string]: any;
27 };
28 /**
29 * Whether to upload the data in chunked streaming mode.
30 * Defaults to true.
31 */
32 chunkedMode?: boolean;
33 /**
34 * A map of header name/header values. Use an array to specify more
35 * than one value. On iOS, FireOS, and Android, if a header named
36 * Content-Type is present, multipart form data will NOT be used.
37 */
38 headers?: {
39 [s: string]: any;
40 };
41}
42export interface FileUploadResult {
43 /**
44 * The number of bytes sent to the server as part of the upload.
45 */
46 bytesSent: number;
47 /**
48 * The HTTP response code returned by the server.
49 */
50 responseCode: number;
51 /**
52 * The HTTP response returned by the server.
53 */
54 response: string;
55 /**
56 * The HTTP response headers by the server.
57 */
58 headers: {
59 [s: string]: any;
60 };
61}
62export interface FileTransferError {
63 /**
64 * One of the predefined error codes listed below.
65 */
66 code: number;
67 /**
68 * URL to the source.
69 */
70 source: string;
71 /**
72 * URL to the target.
73 */
74 target: string;
75 /**
76 * HTTP status code. This attribute is only available when a response
77 * code is received from the HTTP connection.
78 */
79 http_status: number;
80 /**
81 * Response body. This attribute is only available when a response is received from the HTTP connection.
82 */
83 body: string;
84 /**
85 * Either e.getMessage or e.toString.
86 */
87 exception: string;
88}
89/**
90 * @name Transfer
91 *
92 * @description
93 * This plugin allows you to upload and download files.
94 *
95 * @usage
96 * ```typescript
97 * import { Transfer } from 'ionic-native';
98 *
99 *
100 * // Create instance:
101 * const fileTransfer = new Transfer();
102 *
103 * // Upload a file:
104 * fileTransfer.upload(..).then(..).catch(..);
105 *
106 * // Download a file:
107 * fileTransfer.download(..).then(..).catch(..);
108 *
109 * // Abort active transfer:
110 * fileTransfer.abort();
111 *
112 * E.g
113 *
114 * upload(){
115 * const fileTransfer = new Transfer();
116 * var options: any;
117 *
118 * options = {
119 * fileKey: 'file',
120 * fileName: 'name.jpg',
121 * headers: {}
122 * .....
123 * }
124 * fileTransfer.upload("<file path>", "<api endpoint>", options)
125 * .then((data) => {
126 * // success
127 * }, (err) => {
128 * // error
129 * })
130 * }
131 *
132 * // Cordova
133 * declare var cordova: any;
134 *
135 * download() {
136 * const fileTransfer = new Transfer();
137 * let url = 'http://www.example.com/file.pdf';
138 * fileTransfer.download(url, cordova.file.dataDirectory + 'file.pdf').then((entry) => {
139 * console.log('download complete: ' + entry.toURL());
140 * }, (error) => {
141 * // handle error
142 * });
143 * }
144 *
145 * ```
146 *
147 * Note: You will not see your documents using a file explorer on your device. Use adb:
148 *
149 * ```
150 * adb shell
151 * run-as com.your.app
152 * cd files
153 * ls
154 * ```
155 *
156 * To store files in a different/publicly accessible directory, please refer to the following link
157 * https://github.com/apache/cordova-plugin-file#where-to-store-files
158 *
159 * @interfaces
160 * FileUploadOptions
161 * FileUploadResult
162 * FileTransferError
163 */
164export declare class Transfer {
165 /**
166 * Error code rejected from upload with FileTransferError
167 * Defined in FileTransferError.
168 * FILE_NOT_FOUND_ERR: 1 Return when file was not found
169 * INVALID_URL_ERR: 2, Return when url was invalid
170 * CONNECTION_ERR: 3, Return on connection error
171 * ABORT_ERR: 4, Return on aborting
172 * NOT_MODIFIED_ERR: 5 Return on "304 Not Modified" HTTP response
173 * @enum {number}
174 */
175 static FileTransferErrorCode: {
176 FILE_NOT_FOUND_ERR: number;
177 INVALID_URL_ERR: number;
178 CONNECTION_ERR: number;
179 ABORT_ERR: number;
180 NOT_MODIFIED_ERR: number;
181 };
182 private _objectInstance;
183 constructor();
184 /**
185 * Sends a file to a server.
186 *
187 * @param {string} 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.
188 * @param {string} url URL of the server to receive the file, as encoded by encodeURI().
189 * @param {FileUploadOptions} options Optional parameters.
190 * @param {boolean} 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.
191 * @returns {Promise<FileUploadResult>} Returns a Promise that resolves to a FileUploadResult and rejects with FileTransferError.
192 */
193 upload(fileUrl: string, url: string, options?: FileUploadOptions, trustAllHosts?: boolean): Promise<FileUploadResult>;
194 /**
195 * Downloads a file from server.
196 *
197 * @param {string} source URL of the server to download the file, as encoded by encodeURI().
198 * @param {stirng} 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.
199 * @param {boolean} 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.
200 * @param {object} Optional parameters, currently only supports headers (such as Authorization (Basic Authentication), etc).
201 * @returns {Promise<any>} Returns a Promise that resolves to a FileEntry object.
202 */
203 download(source: string, target: string, trustAllHosts?: boolean, options?: {
204 [s: string]: any;
205 }): Promise<any>;
206 /**
207 * Registers a listener that gets called whenever a new chunk of data is transferred.
208 * @param {function} Listener that takes a progress event.
209 */
210 onProgress(listener: (event: ProgressEvent) => any): void;
211 /**
212 * Aborts an in-progress transfer. The onerror callback is passed a FileTransferError
213 * object which has an error code of FileTransferError.ABORT_ERR.
214 */
215 abort(): void;
216}