使用 http MultiPartRequest 将文件上传到服务器
大家好,我希望大家好。
我试图使用以下方式将图像上传到服务器IM: 但这有点工作。所以请帮助我,我真的很感激。
Future<int> uploadFile(imageFile) async {
try {
var request = http.MultipartRequest(
'POST',
Uri.parse('http://10.0.2.2:8000/objects/'),
);
Map<String, String> headers = {"Content-type": "multipart/form-data"};
request.files.add(
http.MultipartFile(
'file',
imageFile.readAsBytes().asStream(),
imageFile.lengthSync(),
contentType: MediaType(
'image',
'jpeg',
),
),
);
request.headers.addAll(headers);
request.fields.addAll({
"app_label": "files",
"app_model": "file",
});
print("request: " + request.toString());
var res = await request.send();
print("This is response:" + res.toString());
return res.statusCode;
} catch (err) {
print(err);
return 502;
}
}
服务器和VSCODE的响应: https://ibb.co/2smtbxj
您可以在此处查看该项目: https://github.com/bruns123/hydra
也同样,如果有人知道如何不知道如何不耕种图像,像“完成”文件。 我可以从画廊或相机拍摄图像,然后您有一个预览,可以发送或保存到画廊。但是我创建了一个避免裁剪图像的选项,但是当我尝试将信息传递给不裁剪时,什么也不会发生。
另外,我想知道我是否可以将所有空隙功能都放在分开的文件中。
希望你有美好的一天!
Hello to everyone i hope you all good.
Im trying to upload a image to a server im using this:
But it dosent work. So please help me, i would really appreciate it.
Future<int> uploadFile(imageFile) async {
try {
var request = http.MultipartRequest(
'POST',
Uri.parse('http://10.0.2.2:8000/objects/'),
);
Map<String, String> headers = {"Content-type": "multipart/form-data"};
request.files.add(
http.MultipartFile(
'file',
imageFile.readAsBytes().asStream(),
imageFile.lengthSync(),
contentType: MediaType(
'image',
'jpeg',
),
),
);
request.headers.addAll(headers);
request.fields.addAll({
"app_label": "files",
"app_model": "file",
});
print("request: " + request.toString());
var res = await request.send();
print("This is response:" + res.toString());
return res.statusCode;
} catch (err) {
print(err);
return 502;
}
}
Response from the server and vscode:
https://ibb.co/2SMTBxJ
You can check the project here:
https://github.com/bRUNS123/hydra
Also if someone know how to put the image without crop like the "finished" file.
I the app you can take an image from the gallery or camera, then you have a preview and you can send or save to the gallery. But i created an option to avoid the crop image, but when i try to pass the information to not crop, nothing happen.
Also i wanna know if i can put all my void functions in a separated file.
Hope you have a great day!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论