uploadFile method

Future<int> uploadFile(
  1. int idx
)

Implementation

Future<int> uploadFile(int idx) async {
	Log("uploadFile: idx = " + idx.toString());

	this.files[idx]['status'] = FileStatus_Uploading;
	this.files[idx]['loadedSize'] = 0;

	//var uploadTime = Math.floor(5000 * Math.random()) + 3000;
	//setTimeout(this.simulateUpload.bind(this), uploadTime, resolve, reject, idx);

	this.files[idx]['cancelToken'] = CancelToken();

	Dio dio = Dio();
	var inputStream = this.files[idx]['file'].openRead();

	try {
		await dio.put(
			this.files[idx]['url'],
			data: this.files[idx]['file'].openRead(),
			//maxContentLength: Infinity,
			//maxBodyLength: Infinity,
			options: Options(
				contentType: this.files[idx]['contentType'],
				headers: {
					'Accept': "*/*",
					'Content-Length': this.files[idx]['fileSize'],
					//'Connection': 'keep-alive',
					//'User-Agent': 'ClinicPlush'
				}
			),
			cancelToken: this.files[idx]['cancelToken'],
			onSendProgress: (int sent, int total) {
				//Log("upload progress: idx = " + idx.toString() +
				//	", sent = " + sent.toString() + ", total = " + total.toString());

				// onSendProgress being called does not necessarily means the file
				// has been successfully saved on the server. The server may reject
				// the upload after all for various reasons.
				//
				this.files[idx]['loadedSize'] = sent;
			}
		);
		Log("upload succeeded");
		return idx;
	}
	on DioError catch (e) {
		Log(e.toString());
		if (CancelToken.isCancel(e)) {
			Log("upload canceled");
			this.files[idx]['status'] = FileStatus_Canceled;
			throw new UploadException(-1);
		}
		else {
			Log("upload failed");
			throw new UploadException(idx);
		}
	}
}