2

I wrote a Dart web app that compiles to Javascript (dart2js), which I package in an APK (PhoneGap) and run as an Android app. I want my app to backup and restore its data from the local file system.

W3C has a File API standard for file IO within a web browser: http://www.w3.org/TR/FileAPI/#introduction.

A popular HTML5 learning website has an article showing how to use the W3C File API: http://www.html5rocks.com/en/tutorials/file/dndfiles/

I tried to use Dart's dart:io library to write to a file from a web page, but got this error:

Failed to load a file dart:io
 Failed to load resource
   dart:io

I'm not sure what caused this error. Maybe dart:io doesn't run in a browser. Maybe dart:io has an undocumented pubspec dependency (I didn't see any dart:io examples that declare its dependencies.)

If you know how to read/write files in a web browser using Dart, please explain how to do it.

Thanks

devdanke
  • 1,309
  • 15
  • 27
  • Dart:io does indeed not run in the browser. From the docs: "The IO library is used for Dart server applications, which run on a stand-alone Dart VM from the command line. This library does not work in browser based applications." – Christophe Herreman Oct 05 '13 at 05:47
  • 2
    Have you tried using the classes in dart:html? Looks like they are all there and you should be able to use them just like in that article. – Dennis Kaselow Oct 05 '13 at 07:39

1 Answers1

4

dart:io is a Server library, you have to use File from dart:html.

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • Thanks for pointing out the file system classes in dart:html. I'm glad they're there. Unfortunately, using them (e.g. new FileSystem().root.createFile("foo.txt") ) triggers an "Aw, snap" in Dartium. If you know of working example code for reading and writing a file, please provide a link. – devdanke Oct 05 '13 at 23:42
  • I think you have to get `FileSystem` with [`requestFileSystem`](http://api.dartlang.org/docs/releases/latest/dart_html/Window.html#requestFileSystem). See also [Exploring the FileSystem APIs](http://www.html5rocks.com/en/tutorials/file/filesystem/). – Alexandre Ardhuin Oct 06 '13 at 05:05
  • I tried using Dart r27487 on Ubuntu 13.04, to create a file in Dartium and Firefox. Neither worked:-( The Dartium error was "The operation failed because it would cause the application to exceed its storage quota.", despite calling window.requestFileSystem(), with size argument up to 100K (which should be enough to create an empty file). The Firefox error was "TypeError: receiver.webkitRequestFileSystem is not a function." I'll try again in a few weeks with a newer Dart version. – devdanke Oct 08 '13 at 02:39