0

I would like to know how can a server side application receive a file (via POST) and then print its contents on the server side.

The most "up to date" related question here was this one: Dart how to upload image

But it is not working anymore (Uncaught Error: type 'String' is not a subtype of type 'HttpBodyFileUpload' of 'fileUploaded').

EDIT:

This is how I send the file (this method is working fine):

import 'dart:html';
import 'dart:async';

HttpRequest request = new HttpRequest();
final _HOST = "127.0.0.1", _PORT = 8123;

Future sendFile(File file) {
    var completer = new Completer(); // No need for a Completer. It will be removed.
    Uri uri = new Uri(host: _HOST, port: _PORT);
    request.open("POST", uri.toString());
    var filename = file.name;
    final FormData formData = new FormData();
    formData.append('file', filename);
    request.onLoadEnd.listen((_) {
        completer.complete(request.response);
    });
    request.send(formData);
    return completer.future;
}

The server side (I'm stuck here):

void _handlePost(HttpRequest req) {
    HttpBodyHandler.processRequest(req).then((body) {
        HttpBodyFileUpload fileUploaded = body.body['file'];
        print(fileUploaded.content);
    });
}
Community
  • 1
  • 1
Felipe
  • 376
  • 2
  • 5
  • 15
  • How do you upload your file? – Robert Nov 14 '14 at 21:32
  • Robert, the file is selected via InputElement, however it is not sent by the form, but via a Dart method. – Felipe Nov 14 '14 at 21:52
  • Yes ... that's what I thought. You are adding the file NAME to the form and not the file. Just change it to `formData.append('file', file);` and it should work. – Robert Nov 14 '14 at 22:15
  • It doesn't work: The argument type 'File' cannot be assigned to the parameter type 'String'. – Felipe Nov 14 '14 at 22:19
  • And I forgot to say that this method is working. The problem is when I'm processing this file on the SERVER SIDE: I need to access its content. – Felipe Nov 14 '14 at 22:25
  • Yes and I am telling you that you are appending it wrong!!! `void append(DOMString name, Blob value, optional DOMString filename);` – Robert Nov 14 '14 at 22:33
  • Robert, ok. So what about that error? – Felipe Nov 14 '14 at 22:34
  • Just send the data correctly and it works. Use the function I posted. – Robert Nov 14 '14 at 22:42
  • Robert, I already tried your code (I changed "filename" to "file") and it didn't work. Please, take a look at comment #4. – Felipe Nov 14 '14 at 22:55
  • you have to change arguments more: `append('file', file, file.name)` – Robert Nov 14 '14 at 22:57
  • @Robert append doesn't accept 3 arguments: FormData.append(String name, String value) → void. – Felipe Nov 15 '14 at 19:12
  • I guess in dart it is `f.appendBlob(name, value, [filename])` then. – Robert Nov 15 '14 at 19:45
  • Yes, I did that a few minutes ago and it is working. I just want to confirm some things here. Thanks! – Felipe Nov 15 '14 at 19:46

1 Answers1

0

You are appending the filename instead of the Blob (File) to your FormData object. In Dart it looks like there is a special function for appending blobs called appendBlob(name, blob, [filename]).

Robert
  • 5,484
  • 1
  • 22
  • 35