TF-296 Fix download file on browser
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:universal_html/html.dart' as html;
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
@@ -72,4 +73,31 @@ class DownloadManager {
|
||||
}
|
||||
return streamController.stream.first;
|
||||
}
|
||||
|
||||
Future<bool> downloadFileForWeb(String downloadUrl, String filename, String basicAuth) async {
|
||||
final headerParam = Map<String, String>();
|
||||
headerParam[HttpHeaders.authorizationHeader] = basicAuth;
|
||||
headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader;
|
||||
|
||||
http.Response res = await http.get(Uri.parse(downloadUrl), headers: headerParam);
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
final blob = html.Blob([res.bodyBytes]);
|
||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||
final anchor = html.document.createElement('a') as html.AnchorElement
|
||||
..href = url
|
||||
..style.display = 'none'
|
||||
..download = filename;
|
||||
html.document.body?.children.add(anchor);
|
||||
|
||||
anchor.click();
|
||||
|
||||
html.document.body?.children.remove(anchor);
|
||||
html.Url.revokeObjectUrl(url);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
typedef OnCancelDownloadActionClick = void Function();
|
||||
@@ -57,13 +58,13 @@ class DownloadingFileDialogBuilder {
|
||||
),
|
||||
)),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
if (_onCancelDownloadActionClick != null) {
|
||||
_onCancelDownloadActionClick!();
|
||||
}},
|
||||
child: Text(_actionText, style: TextStyle(fontSize: 17.0, color: AppColor.appColor)),
|
||||
)
|
||||
if (_actionText.isNotEmpty)
|
||||
Padding(
|
||||
padding: EdgeInsets.only(bottom: kIsWeb ? 16 : 0, top: kIsWeb ? 16 : 0),
|
||||
child: TextButton(
|
||||
onPressed: () => _onCancelDownloadActionClick?.call(),
|
||||
child: Text(_actionText, style: TextStyle(fontSize: 17.0, color: AppColor.appColor)),
|
||||
))
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ dependencies:
|
||||
# collection
|
||||
collection: 1.15.0
|
||||
|
||||
universal_html: 2.0.8
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
Reference in New Issue
Block a user