Kar*_*rar 11 multipartform-data dart flutter
我正在尝试通过 flutter 中的 multiPartRequest 将图像发送到服务器,当我将图像添加到请求文件中时,一旦我想指定 MediaType 的内容类型,就会出现编译时错误,告诉我 MediaType 类没有定义。
我该如何解决这个问题?
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);
Run Code Online (Sandbox Code Playgroud)
Min*_*rid 23
你需要导入:
import 'package:http_parser/http_parser.dart';
Run Code Online (Sandbox Code Playgroud)
看看这个是否有效
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!");
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5077 次 |
| 最近记录: |