sager89
sager89

Reputation: 980

Dart Html - Convert Blob to File

I'm attempting to write a test for some dart:html code.

I have a method with a File parameter (html File, not io File).

testFile(File file)

I'm able to create a Blob with the needed data for the file (minus file name, date, etc.), but it appears there is no way to create File objects in dart:html, as it's reserved for internal use in html_dartium.dart.

factory File._() { throw new UnsupportedError("Not supported"); }

Is there any other way to create an HTML File object?

I've seen FileReaders mentioned, but the results from those is either a String or uint8list.

Upvotes: 3

Views: 6288

Answers (2)

sager89
sager89

Reputation: 980

After further research, I achieved what I was looking for with the following:

  List<String> file_contents = ["test\n"];
  Blob blob = new Blob(file_contents, 'text/plain', 'native');

  FileSystem _filesystem = await window.requestFileSystem(1024 * 1024, persistent: false);
  FileEntry fileEntry = await _filesystem.root.createFile('dart_test.csv');
  FileWriter fw = await fileEntry.createWriter();
  fw.write(blob);
  File file = await fileEntry.file();

Upvotes: 2

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657018

Something like

  Blob response = _downloadRequest.response;
  final FileReader reader = new FileReader();

  reader.onLoad.listen((e) {
        _handleData(reader);
      });
  reader.readAsArrayBuffer(response);

See Downloading a file using Dart GDrive api with authorized GET request

Upvotes: 0

Related Questions