Dart Html – Convert Blob to File

Issue

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.

Solution

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();

Answered By – sager89

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *