Reputation: 980
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
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
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