Dart语言:从POST接收文件并在服务器上打印其内容

时间:2014-11-14 20:50:08

标签: dart server

我想知道服务器端应用程序如何通过POST接收文件,然后在服务器端打印其内容。

此处最“最新”的相关问题是:Dart how to upload image

但它不再起作用了(Uncaught Error:类型'String'不是'fileUploaded''HttpBodyFileUpload'类型的子类型。)

编辑:

这是我发送文件的方式(此方法工作正常):

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;
}

服务器端(我被困在这里):

void _handlePost(HttpRequest req) {
    HttpBodyHandler.processRequest(req).then((body) {
        HttpBodyFileUpload fileUploaded = body.body['file'];
        print(fileUploaded.content);
    });
}

1 个答案:

答案 0 :(得分:0)

您要将文件名而不是Blob(文件)附加到FormData对象。在Dart中,看起来有一个特殊的函数用于附加名为appendBlob(name, blob, [filename])的blob。