Open file in web

This commit is contained in:
Christian Pauly 2020-04-10 18:25:47 +02:00
parent 80386946e5
commit 6aa6d0e911
3 changed files with 21 additions and 3 deletions

View File

@ -1,15 +1,32 @@
import 'dart:io'; import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:flutter/foundation.dart';
import 'package:open_file/open_file.dart'; import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:universal_html/prefer_universal/html.dart' as html;
import 'package:mime_type/mime_type.dart';
extension MatrixFileExtension on MatrixFile { extension MatrixFileExtension on MatrixFile {
void open() async { void open() async {
Directory tempDir = await getTemporaryDirectory(); Directory tempDir = await getTemporaryDirectory();
final file = File(tempDir.path + "/" + path.split("/").last); final file = File(tempDir.path + "/" + path.split("/").last);
file.writeAsBytesSync(bytes); file.writeAsBytesSync(bytes);
await OpenFile.open(file.path); if (kIsWeb) {
final fileName = path.split('/').last;
final mimeType = mime(fileName);
var element = html.document.createElement('a');
element.setAttribute(
'href', html.Url.createObjectUrlFromBlob(html.Blob([bytes])));
element.setAttribute('target', "new");
element.setAttribute('type', mimeType);
element.style.display = 'none';
html.document.body.append(element);
element.click();
element.remove();
} else {
await OpenFile.open(file.path);
}
return; return;
} }
} }

View File

@ -348,12 +348,12 @@ packages:
source: hosted source: hosted
version: "0.9.6+3" version: "0.9.6+3"
mime_type: mime_type:
dependency: transitive dependency: "direct main"
description: description:
name: mime_type name: mime_type
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.4" version: "0.2.7"
multi_server_socket: multi_server_socket:
dependency: transitive dependency: transitive
description: description:

View File

@ -52,6 +52,7 @@ dependencies:
photo_view: ^0.9.2 photo_view: ^0.9.2
flutter_sound: ^2.1.1 flutter_sound: ^2.1.1
open_file: ^3.0.1 open_file: ^3.0.1
mime_type: ^0.2.7
intl: ^0.16.0 intl: ^0.16.0
intl_translation: ^0.17.9 intl_translation: ^0.17.9