我尝试使用发布请求将视频文件上传到我的服务器。
var file = new File(videoPath);
var uri = Uri.parse(tokenizedUri);
HttpClientRequest request = await new HttpClient().postUrl(uri);
await request.addStream(file.openRead());
var response = await request.close();
response.transform(utf8.decoder).forEach((string) {
print(string); // handle data
});
但服务器无法获得它。为什么呢?
答案 0 :(得分:3)
正确的方法是使用MultipartRequest:
var uri = Uri.parse(url);
var request = new MultipartRequest("POST", uri);
var multipartFile = await MultipartFile.fromPath("package", videoPath);
request.files.add(multipartFile);
StreamedResponse response = await request.send();
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
答案 1 :(得分:0)
您可以使用 Dio 包。它支持大文件,对我来说效果很好。
sendFile(String kMainUrl, XFile file) async {
String filePath = file.path;
String fileName = 'any name';
try {
FormData formData = FormData.fromMap({
"file":
await MultipartFile.fromFile(filePath, filename:fileName),
});
Response response =
await Dio().post(kMainUrl, data: formData);
print("File upload response: $response");
print(response.data['message']);
} catch (e) {
print("Exception Caught: $e");
}
}