flutter app untuk unitstock
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

154 lines
4.8 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:typed_data';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:http/http.dart';
  7. class file_Trans_Handler {
  8. // double _progress = 0;
  9. String _path = '';
  10. StreamSubscription dlulStream;
  11. String _error = '';
  12. get path => _path;
  13. get error => _error;
  14. StreamController _progress = new StreamController<double>();
  15. Stream<double> get progress =>_progress.stream;
  16. var client = new Client();
  17. downloadFile(String fileName,String link) async {
  18. StreamedResponse _response;
  19. List<int> _bytes = [];
  20. int _total = 0;
  21. print('Start Download');
  22. _progress.add(null);
  23. try {
  24. Request req = new Request('GET', Uri.parse(link));
  25. // req.headers = '';
  26. _response = await client.send(req).timeout(
  27. Duration(seconds: 20));
  28. // _response = await client.get('$link',headers: 'application/json')
  29. _total = _response.contentLength;
  30. print('${_total / 1024} KB');
  31. dlulStream = _response.stream.listen((value) {
  32. _bytes.addAll(value);
  33. _progress.add(((_bytes.length / _total)));
  34. })
  35. ..onDone(() async {
  36. _progress.add(0.0);
  37. print('Finish Download');
  38. final file = File(
  39. "${(await getApplicationDocumentsDirectory()).path}/$fileName");
  40. await file.writeAsBytes(_bytes);
  41. _path = file.path;
  42. })
  43. ..onError((e) async {
  44. print('Error Download, $e');
  45. _progress.add(-1.0);
  46. _error = e.toString();
  47. });
  48. }
  49. catch(e){
  50. print('Error Download, $e');
  51. _progress.add(-1.0);
  52. _error = e.toString();
  53. }
  54. }
  55. uploadFile(String fileName,String link,String company,String cabang_id) async{
  56. final file = File(
  57. "${(await getApplicationDocumentsDirectory()).path}/$fileName");
  58. if(file.existsSync()){
  59. Uint8List byte = file.readAsBytesSync();
  60. // print("file size ${file.lengthSync()/1024}");
  61. try{
  62. var _reponse = await client.post(
  63. '$link', headers: {'Content-type': 'application/json'},
  64. body: json.encode({"byte":byte,"cabangId":cabang_id,"company":company}));
  65. print('File send ${file.lengthSync()/1024} KB');
  66. final Map data = JsonDecoder().convert(_reponse.body);
  67. // print(_reponse.body);
  68. // if(data['STATUS']==1){
  69. // return {"STATUS":1,"DATA":'File send ${file.lengthSync()/1024} KB'};
  70. // }
  71. // else {
  72. return data;
  73. // }
  74. }
  75. catch(e){
  76. print(e);
  77. return {"STATUS":0,"DATA":'Request timeout. Make sure server is up and running'};
  78. }
  79. }
  80. else{
  81. return {"STATUS":0,"DATA":'No such file'};
  82. }
  83. }
  84. unPackDb(String link,String company,String cabang_id,String dbPath) async{
  85. try{
  86. var _reponse = await client.post(
  87. '$link', headers: {'Content-type': 'application/json'},
  88. body: json.encode({"cabangId":cabang_id,"company":company,"dbPath":dbPath}));
  89. final Map data = JsonDecoder().convert(_reponse.body);
  90. return data;
  91. }
  92. catch(e){
  93. print(e);
  94. return {"STATUS":0,"DATA":'Upload timeout. Make sure server is up and running'};
  95. }
  96. }
  97. submitDb(String link,String company,String stock_id) async{
  98. try{
  99. var _reponse = await client.post(
  100. '$link', headers: {'Content-type': 'application/json'},
  101. body: json.encode({"stockTakingId":stock_id,"company":company}));
  102. final Map data = JsonDecoder().convert(_reponse.body);
  103. return data;
  104. }
  105. catch(e){
  106. print(e);
  107. return {"STATUS":0,"DATA":'Request timeout. Make sure server is up and running'};
  108. }
  109. }
  110. uploadMultipart(String fileName,String link)async{
  111. StreamedResponse _response;
  112. List<int> _bytes = [];
  113. int _total = 0;
  114. var request = MultipartRequest('POST', Uri.parse(link));
  115. request.files.add(
  116. await MultipartFile.fromPath(
  117. 'picture',
  118. "${(await getApplicationDocumentsDirectory()).path}/$fileName"
  119. )
  120. );
  121. _response = await client.send(request);
  122. _total = File("${(await getApplicationDocumentsDirectory()).path}/$fileName").lengthSync();
  123. dlulStream = _response.stream.listen((value) {
  124. _bytes.addAll(value);
  125. print('upload ${_bytes.length/_total}');
  126. _progress.add(((_bytes.length / _total)));
  127. })
  128. ..onDone(() async {
  129. _progress.add(0.0);
  130. print('Finish Download');
  131. final file = File(
  132. "${(await getApplicationDocumentsDirectory()).path}/$fileName");
  133. await file.writeAsBytes(_bytes);
  134. _path = file.path;
  135. })
  136. ..onError((e) async {
  137. print('Error Download, $e');
  138. _progress.add(-1.0);
  139. _error = e.toString();
  140. });
  141. }
  142. cancel()async{
  143. client?.close();
  144. await dlulStream?.cancel();
  145. _progress?.close();
  146. }
  147. }