【发布时间】:2021-09-19 07:33:39
【问题描述】:
我正在尝试创建一个服务器以在本地运行,以便我的应用程序在调试时上传一些文件。非常简单,完整的源代码是:
import 'dart:io';
void main(List<String> arguments) async {
const port = 8080;
final server = await HttpServer.bind(InternetAddress.anyIPv4, port);
server.listen((request) async {
if (request.uri.path != '/save_screenshot' || request.method != 'POST') {
request.response.statusCode = 404;
request.response.close();
return;
}
// TODO: read the file
request.response.statusCode = 200;
request.response.close();
});
print('screenshot server listening on $port.');
}
在代码中有一个 TODO 注释,我想从 HTTPRequest 读取文件,我搜索了一下,找不到要复制的示例。有谁知道如何从 HTTPRequest 读取文件?
这是它的发送方式(在客户端):
final url = 'http://<local_ip_address>:8080/save_screenshot';
var request = http.MultipartRequest('POST', Uri.parse(url));
request.files.add(
http.MultipartFile.fromBytes(
'file',
screenshot.bytes,
filename: 'screenshot.png',
),
);
await request.send();
【问题讨论】: