|
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'dart:typed_data';
- import 'package:assetstock/util/prefsKey.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:http/http.dart';
- import 'package:path/path.dart';
- import 'package:async/async.dart';
- import 'dart:io';
- import 'utils.dart';
-
- class file_Trans_Handler {
- // double _progress = 0;
- Util util = new Util();
- String _path = '';
- StreamSubscription dlulStream;
- String _error = '';
- String _success = '';
- bool _finish = false;
- get isFinish => _finish;
- get path => _path;
- get error => _error;
- get success => _success;
- StreamController _progress = new StreamController<double>();
- Stream<double> get progress =>_progress.stream;
- var client = new Client();
- downloadFile(String fileName,String link) async {
- StreamedResponse _response;
- List<int> _bytes = [];
- int _total = 0;
- print('Start Download');
- _progress.add(null);
- try {
- Request req = new Request('GET', Uri.parse(link));
- // req.headers = '';
- _response = await client.send(req).timeout(
- Duration(seconds: 20));
- // _response = await client.get('$link',headers: 'application/json')
- _total = _response.contentLength;
- print('${_total / 1024} KB');
- dlulStream = _response.stream.listen((value) {
- _bytes.addAll(value);
- _progress.add(((_bytes.length / _total)));
- })
- ..onDone(() async {
- _progress.add(0.0);
- print('Finish Download');
- final file = File(
- "${(await getApplicationDocumentsDirectory()).path}/$fileName");
- await file.writeAsBytes(_bytes);
- _path = file.path;
- })
- ..onError((e) async {
- print('Error Download, $e');
- _progress.add(-1.0);
- _error = e.toString();
- });
- }
- catch(e){
- print('Error Download, $e');
- _progress.add(-1.0);
- _error = e.toString();
- }
- }
-
- uploadFile(String fileName,String link,String user,context) async{
- StreamedResponse _response;
- int _total;
- List<int> _bytes = [];
- _finish = false;
- final file = File(
- "${(await getApplicationDocumentsDirectory()).path}/$fileName");
- _total = file.lengthSync();
- print(_total/1024);
- try{
- var request = MultipartRequest('POST', Uri.parse(link));
- request.files.add(await MultipartFile.fromPath('sqliteDb', file.path));
- request.fields[keyClass.user] = user;
-
- // _response = await client.send(request).timeout(Duration(seconds: 8));
- print(["sadasdasda",await util.checkinternet()]);
- if(await util.checkinternet()){
- util.showLoading(context,dissmissable: false,onwillpop: ()async{return false;});
- _response = await client.send(request);
- print("MASIH JALAN");
- if(_response.statusCode==200){
- _error = '';_success = '';
- dlulStream = _response.stream.listen((value) {
- _bytes.addAll(value);
- _progress.add((_bytes.length / _total));
- try{
- var result = jsonDecode(utf8.decode(value));
- if(result['STATUS']==0) {
- _error = result['DATA'];
- _finish = true;
- }
- else if(result['STATUS']==1){
- _error = '';
- _success = result['DATA'];
- _finish = true;
- }
- }catch(e){
- }
- })
- ..onDone(()async {
- Navigator.pop(context);
- client?.close();
- _finish = true;
-
- print("sdasdasdsd $_error");
- if(_error!=''){
- _progress.add(-1.0);
- print('Error Upload, $_error');
- }
- else{
- _progress.add(1.0);
- print('Finish Upload');
- print('done');
- }
- })
- ..onError((e){
- Navigator.pop(context);
- client?.close();
- print('Error Upload, $e');
- _progress.add(-1.0);
- _error = e.toString();
- });
- }
- else{
- Navigator.pop(context);
- client?.close();
- var resBody = await _response.stream.bytesToString();
- _progress.add(-1.0);
- _finish = true;
- _error = resBody;
- }
- }
- else{
- client?.close();
- _progress.add(-1.0);
- _finish = true;
- _error = "Check Internet Connection";
- }
- }
- // on TimeoutException{
- // client?.close();
- // print(['error timed out']);
- // Navigator.pop(context);
- // _progress.add(-1.0);
- // _finish = true;
- // _error = "Request timed out. Please check connection";
- // }
- catch(e){
- client?.close();
- print(['error',e]);
- _finish = true;
- _progress.add(-1.0);
- _error = e.toString();
- }
- }
- cancel()async{
- client?.close();
- await dlulStream?.cancel();
- _progress?.close();
- }
- }
|