Mike Mitterer
Mike Mitterer

Reputation: 7180

Upload file to server with Dart

I am using something like this for my tests:

_sendFormData(final File file) {
    final HttpRequest httprequest = new HttpRequest();
    final String filename = file.name;

    httprequest.open('POST', "http://localhost:8080/api/file/upload");
    httprequest.on.readyStateChange.add((e) {
      if (httprequest.readyState == 4 && httprequest.status == 200) {
        window.alert("upload complete");
      }
    });

    print("Filename: ${filename}");

    final FormData formData = new FormData();
    formData.append('file', null, filename);
    httprequest.send(formData);
}

And yes I know it's only the filename that I am sending, but how can I send the whole file to the server?

Upvotes: 2

Views: 1446

Answers (2)

Tim Rasim
Tim Rasim

Reputation: 684

They introduced a new function to append files with a given fileName:

FormData.appendBlob(String name, Blob value, [String filename]) 

Upvotes: 4

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76193

You should be able to upload file with :

formData.append('file', file);

Upvotes: 6

Related Questions