Skip to content

Instantly share code, notes, and snippets.

@mateisilviu
Last active August 25, 2023 19:29
Show Gist options
  • Save mateisilviu/5ef432baf964942a9e98d01fd51a8257 to your computer and use it in GitHub Desktop.
Save mateisilviu/5ef432baf964942a9e98d01fd51a8257 to your computer and use it in GitHub Desktop.
Flutter Web download pdf as files from Firebase storage
import 'dart:html' as html;
import 'dart:typed_data';
import 'package:cross_file/cross_file.dart';
import 'package:firebase_storage/firebase_storage.dart';
downloadFileWeb(String url,String fileName) async {
final httpsReference = FirebaseStorage.instance.refFromURL(url);
try {
const oneMegabyte = 1024 * 1024;
final Uint8List? data = await httpsReference.getData(oneMegabyte);
// Data for "images/island.jpg" is returned, use this as needed.
XFile.fromData(data!,
mimeType: "application/octet-stream", name: fileName + ".pdf")
.saveTo("C:/"); // here Path is ignored
} on FirebaseException catch (e) {
// Handle any errors.
}
// for other platforms see this solution : https://firebase.google.com/docs/storage/flutter/download-files#download_to_a_local_file
}
previewPDFFile(url) {
html.window.open(url,"_blank"); //opens pdf in new tab
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment