Flutter 如何在flatter中上传图像
我是个新手。我想将图像和一些详细信息上传到我的API。关于Flutter 如何在flatter中上传图像,flutter,Flutter,我是个新手。我想将图像和一些详细信息上传到我的API。关于“消息”我有错误:“未定义变量:扩展名”。我的图像文件是普通图像文件不是base64编码图像 以下是我的开放式图库代码: _openGallery(BuildContext context) async { var picture = await ImagePicker.pickImage(source: ImageSource.gallery); this.setState(() { imageFile =
“消息”我有错误:“未定义变量:扩展名”
。我的图像文件是普通图像文件不是base64编码图像
以下是我的开放式图库代码:
_openGallery(BuildContext context) async {
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState(() {
imageFile = picture;
Navigator.of(context).pop();
});
}
以下是上传的代码:
Future upload(BuildContext context) async {
try {
String url = Uri.encodeFull("https://API_URL");
http.Response response = await http.post(url,
body: {
'photo': imageFile.path,
'title': titleController.text,
'description': descriptionController.text,
}
);
print(response.body);
} catch(e) {
print(e);
}
}
如何解决此错误?试试这个
Map<String, String> headers = { "Authorization": 'token'};
var request = http.MultipartRequest('POST', Uri.parse(url));
request.headers.addAll(headers);
request.files.add(http.MultipartFile.fromBytes('photo', imageFile.readAsBytesSync(), filename: 'photo.jpg'));
request.fields['title'] = titleController.text;
request.fields['description'] = descriptionController.text;
final response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
Map头={“授权”:“令牌”};
var request=http.MultipartRequest('POST',Uri.parse(url));
request.headers.addAll(headers);
request.files.add(http.MultipartFile.fromBytes('photo',imageFile.readAsBytesSync(),文件名:'photo.jpg');
request.fields['title']=titleController.text;
request.fields['description']=descriptionController.text;
最终响应=等待请求。发送();
final respStr=wait response.stream.bytesToString();
打印(respStr);
试试这个
Map<String, String> headers = { "Authorization": 'token'};
var request = http.MultipartRequest('POST', Uri.parse(url));
request.headers.addAll(headers);
request.files.add(http.MultipartFile.fromBytes('photo', imageFile.readAsBytesSync(), filename: 'photo.jpg'));
request.fields['title'] = titleController.text;
request.fields['description'] = descriptionController.text;
final response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
Map头={“授权”:“令牌”};
var request=http.MultipartRequest('POST',Uri.parse(url));
request.headers.addAll(headers);
request.files.add(http.MultipartFile.fromBytes('photo',imageFile.readAsBytesSync(),文件名:'photo.jpg');
request.fields['title']=titleController.text;
request.fields['description']=descriptionController.text;
最终响应=等待请求。发送();
final respStr=wait response.stream.bytesToString();
打印(respStr);
如何运行此功能?错误消息到底是从哪里来的?谢谢你在我按upload之后的回复。我收到此错误“消息”:“未定义变量:扩展名在哪里按上载?在按钮处,我没有显示代码,因为我无法对所有代码进行查询。在使用名为extension
的变量时,是否有任何代码?如何运行此操作?”?错误消息到底是从哪里来的?谢谢你在我按upload之后的回复。我收到这个错误“消息”:“未定义变量:扩展名在哪里按上载?在那里的按钮,我没有显示代码,因为我不能把所有代码都放在问题中。您是否有任何代码,您正在使用一个名为扩展名
的变量?这与错误“未定义变量:扩展名”有什么关系“?错误是因为没有收到图像这与错误“Undefined variable:extension”有什么关系?错误是因为没有收到图像。”