如何将图像发送到 dart/flutter 中的 api?
How to send an image to an api in dart/flutter?
我看到了其他问题,但这不是我想要的,我不想将图像上传到服务器,我不想转换为 base64...
我只想 post 表单数据或其他文件中的文件并获取返回的信息。
我有这个,但没用:
void onTakePictureButtonPressed() {
takePicture().then((String filePath) {
if (mounted) {
setState(() {
imagePath = filePath;
videoController?.dispose();
videoController = null;
});
http.post('http://ip:8082/composer/predict', headers: {
"Content-type": "multipart/form-data",
}, body: {
"image": filePath,
}).then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});
if (filePath != null) showInSnackBar('Picture saved to $filePath');
}
});
}
最简单的方法是 post 像 this post 中那样的多部分请求,然后 post 将其发送到服务器。
确保在文件开头导入这些:
import 'package:path/path.dart';
import 'package:async/async.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'dart:convert';
在您的代码中的某处添加此 class:
upload(File imageFile) async {
// open a bytestream
var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
// get file length
var length = await imageFile.length();
// string to uri
var uri = Uri.parse("http://ip:8082/composer/predict");
// create multipart request
var request = new http.MultipartRequest("POST", uri);
// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(imageFile.path));
// add file to multipart
request.files.add(multipartFile);
// send
var response = await request.send();
print(response.statusCode);
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
然后上传使用:
upload(File(filePath));
在您的代码中:
void onTakePictureButtonPressed() {
takePicture().then((String filePath) {
if (mounted) {
setState(() {
imagePath = filePath;
videoController?.dispose();
videoController = null;
});
// initiate file upload
Upload(File(filePath));
if (filePath != null) showInSnackBar('Picture saved to $filePath');
}
});
}
import 'package:dio/dio.dart'; //From 3.x.x version
uploadImage(){
var formData = FormData();
formData.files.add(MapEntry("Picture", await MultipartFile.fromFile(data.foto.path, filename: "pic-name.png"), ));
var response = await dio.client.post('v1/post', data: formdata);
}
如果您要将图像发送到 PHP Laravel 服务器。尝试在将 size of the image
发送到服务器时减少它。我使用 Image Picker 包来减小图像的大小。
var image = await ImagePicker.pickImage(source: imageSource, imageQuality: 50, maxHeight: 500.0, maxWidth: 500.0);
然后使用该图像创建一个多部分文件,将其添加到表单数据,并使用 post 请求和 dio
库将表单数据发送到服务器。请参阅 @Elialber Lopes
发送数据的答案。
它对我有用。
我看到了其他问题,但这不是我想要的,我不想将图像上传到服务器,我不想转换为 base64...
我只想 post 表单数据或其他文件中的文件并获取返回的信息。
我有这个,但没用:
void onTakePictureButtonPressed() {
takePicture().then((String filePath) {
if (mounted) {
setState(() {
imagePath = filePath;
videoController?.dispose();
videoController = null;
});
http.post('http://ip:8082/composer/predict', headers: {
"Content-type": "multipart/form-data",
}, body: {
"image": filePath,
}).then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});
if (filePath != null) showInSnackBar('Picture saved to $filePath');
}
});
}
最简单的方法是 post 像 this post 中那样的多部分请求,然后 post 将其发送到服务器。
确保在文件开头导入这些:
import 'package:path/path.dart';
import 'package:async/async.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'dart:convert';
在您的代码中的某处添加此 class:
upload(File imageFile) async {
// open a bytestream
var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
// get file length
var length = await imageFile.length();
// string to uri
var uri = Uri.parse("http://ip:8082/composer/predict");
// create multipart request
var request = new http.MultipartRequest("POST", uri);
// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(imageFile.path));
// add file to multipart
request.files.add(multipartFile);
// send
var response = await request.send();
print(response.statusCode);
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
然后上传使用:
upload(File(filePath));
在您的代码中:
void onTakePictureButtonPressed() {
takePicture().then((String filePath) {
if (mounted) {
setState(() {
imagePath = filePath;
videoController?.dispose();
videoController = null;
});
// initiate file upload
Upload(File(filePath));
if (filePath != null) showInSnackBar('Picture saved to $filePath');
}
});
}
import 'package:dio/dio.dart'; //From 3.x.x version
uploadImage(){
var formData = FormData();
formData.files.add(MapEntry("Picture", await MultipartFile.fromFile(data.foto.path, filename: "pic-name.png"), ));
var response = await dio.client.post('v1/post', data: formdata);
}
如果您要将图像发送到 PHP Laravel 服务器。尝试在将 size of the image
发送到服务器时减少它。我使用 Image Picker 包来减小图像的大小。
var image = await ImagePicker.pickImage(source: imageSource, imageQuality: 50, maxHeight: 500.0, maxWidth: 500.0);
然后使用该图像创建一个多部分文件,将其添加到表单数据,并使用 post 请求和 dio
库将表单数据发送到服务器。请参阅 @Elialber Lopes
发送数据的答案。
它对我有用。