miguelpruivo / flutter_file_picker

File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.
MIT License
1.35k stars 673 forks source link

Feature: saveFile on web #1540

Closed akshaybabloo closed 4 months ago

akshaybabloo commented 4 months ago

The saveFile() method on the web can just download file. This is what I have under my stub

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:web/web.dart';

void saveFile({
  String? dialogTitle,
  String? fileName,
  String? initialDirectory,
  FileType type = FileType.any,
  List<String>? allowedExtensions,
  Uint8List? bytes,
  bool lockParentWindow = false,
}) async {
  final data = bytes!.toJS;
  final blob = Blob([data].toJS, BlobPropertyBag(type: 'application/json'));
  final url = URL.createObjectURL(blob);

  final anchor = HTMLAnchorElement()
    ..href = url
    ..setAttribute("download", fileName ?? 'file')
    ..click();
  URL.revokeObjectURL(url);
}

Note: I have hardcoded application/json because I only need the JSON.

github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 7 days with no activity.

github-actions[bot] commented 4 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.