我有一个图像列表作为字节,我想把它作为一个post request在flutter web。
首先,我如何发送文件格式的数据,就像在flutter web中的android/iOS图像选择器
其次,如果我必须发送以下代码中的附件,我需要在multipart in flutter web
中做哪些更改?如果有人能帮我解决这个问题。
如果您需要我这边的进一步信息,请告诉我
dataInsert.dart
File _file = File("zz");
Uint8List webImage = Uint8List(10);
else if (kIsWeb) {
final imagefile =
await picker.pickImage(source: ImageSource.gallery, maxWidth: 600);
if (imagefile != null) {
var f = await imagefile.readAsBytes();
setState(() {
_file = File("a");
webImage = f;
_storedImageWeb.add(webImage);
});
}
}
下面是运行上述代码时webImage的值:
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 226, 2, 40, 73, 67, 67, 95, 80, 82, 79, 70, 73, 76, 69, 0, 1, 1, 0, 0, 2, 24, 0, 0, 0, 0, 4, 48, 0, 0, 109, 110, 116, 114, 82, 71, 66, 32, 88, 89, 90, 32, 0, 0, 0, 0, 0, 0, 0, 0]
Multipart.dart
Future<void> addUser(
ProjectDetail stnotification, List<Uint8List> imagecheckb) async {
Map<String, String> headers = {
"Content-Type": "charset=utf-8",
"Content-type": "application/json"
};
var uri = Uri.parse('http://localhost:4000/check/upload');
try {
var request = http.MultipartRequest('POST', uri);
request.headers.addAll(headers);
for (int i = 0; i <= imagecheckb.length - 1; i++) {
http.MultipartFile multipartFile = await http.MultipartFile.fromBytes(
'attachments', imagecheckb[i].cast());
request.files.add(multipartFile);
}
这是一个从multipart发送文件/图像列表的方法。
await Future.forEach(
files,
(file) async => {
request.files.add(
http.MultipartFile(
'files',
(http.ByteStream(file.openRead())).cast(),
await file.length(),
filename: basename(file.path),
),
)
},
);
您可以按索引处理图像列表:
Future addProduct(String name, String description, List<File> images) async{
final request = await http.MultipartRequest(
'POST',
Uri.parse('$baseUrl/products/add-product'),
);
request.headers.addAll(header);
request.fields['Name'] = name;
request.fields['Description'] = description;
for(int i = 0; i < images.length; i++){
final f = await http.MultipartFile.fromPath('Photos[$i]',images[i].path);
request.files.add(f);
}
final responseStream = await request.send();
final response = await http.Response.fromStream(responseStream);
if(response.statusCode == 200) {
print("success")
} else {
print('failed')
}
});