Flutter中不能指定MultiPartFile的内容类型
can not specify the content type of MultiPartFile in Flutter
我正在尝试通过 flutter 中的 multiPartRequest 将图像发送到服务器,当我将图像添加到请求文件中时,一旦我想指定 MediaType 的内容类型,
出现编译时错误,告诉我未定义 MediaType class。
我该如何解决这个问题?
http.MultipartRequest multipartRequest = new http.MultipartRequest('POST',url);
http.MultipartFile file = new http.MultipartFile.fromBytes('file', await
image.readAsBytes(),contentType: MediaType('image','jpg)); // MediaType class is not defined
multipartRequest.files.add(file);
看看这个是否有效
uploadFile() async {
var postUri = Uri.parse("<APIUrl>");
var request = new http.MultipartRequest("POST", postUri);
request.fields['user'] = 'blah';
request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri("<path/to/file").readAsBytes(), contentType: new MediaType('image', 'jpeg')))
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
});
}
我找到答案了,就是必须导入http解析器包
您需要导入:
import 'package:http_parser/http_parser.dart';
我正在尝试通过 flutter 中的 multiPartRequest 将图像发送到服务器,当我将图像添加到请求文件中时,一旦我想指定 MediaType 的内容类型, 出现编译时错误,告诉我未定义 MediaType class。
我该如何解决这个问题?
http.MultipartRequest multipartRequest = new http.MultipartRequest('POST',url);
http.MultipartFile file = new http.MultipartFile.fromBytes('file', await
image.readAsBytes(),contentType: MediaType('image','jpg)); // MediaType class is not defined
multipartRequest.files.add(file);
看看这个是否有效
uploadFile() async {
var postUri = Uri.parse("<APIUrl>");
var request = new http.MultipartRequest("POST", postUri);
request.fields['user'] = 'blah';
request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri("<path/to/file").readAsBytes(), contentType: new MediaType('image', 'jpeg')))
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
});
}
我找到答案了,就是必须导入http解析器包
您需要导入:
import 'package:http_parser/http_parser.dart';