嗨,我正在尝试通过颤振应用程序将文件上传到服务器,但文件始终为空。它工作正常,之后停止工作。
这是我的代码:
static uploadImageToServer(String image) async {
File imageFile = new File(image);
Map<String, String> headers = {
'Content-Type': 'multipart/form-data',
'Accept-Charset': 'UTF-8'
};
var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length = await imageFile.length();
print(imageFile.path);
var uri = Uri.parse("http://planning.test/test_image");
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile('File', stream, length,
filename: "test_image_failure.jpg");
request.headers.addAll(headers);
request.fields['Destination'] = '/';
print("**********");
print(multipartFile.filename);
print(uri);
print(multipartFile.length);
print("**********");
request.files.add(multipartFile);
print(request.contentLength);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
并带有 dio 包
_dioUpload(String imageFile)async {
var dio = Dio();
var formData = FormData();
formData.files.add(MapEntry(
"File",
await MultipartFile.fromFile(imageFile, filename: "xx.png"),
));
print(formData.files.first.value.length);
var response = await dio.post(
"http://planning.test/test_image",
data: formData,
onSendProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + "%");
}
},
);
print(response);
}
我不明白发生了什么一切看起来都很好并且文件存在。
谢谢