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

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

本文介绍了Dart语言:从POST接收文件并在服务器上打印其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是它不工作了(未捕获的错误:类型'String'不是'fileUploaded'类型'HttpBodyFileUpload'的子类型)。

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

编辑:

这是我如何发送文件(这种方法工作正常):

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


推荐答案

的Blob(文件)到你的 FormData 对象。在Dart中,它看起来像有一个特殊的函数,用于追加称为 appendBlob(name,blob,[filename])

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]).

这篇关于Dart语言:从POST接收文件并在服务器上打印其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:29