如何使用chopper库将图像上传到服务器?我尝试在Google上进行搜索,但无法获得搜索结果。
我尝试过的是
创建斩波器客户端的功能句柄
static ApiService create(String accessToken) {
final client = ChopperClient(
baseUrl: 'http://192.168.43.125:8000/api',
services: [
_$ApiService(),
],
converter: JsonConverter(),
interceptors: [
HeadersInterceptor({
"Content-Type": "application/x-www-form-urlencoded",
'Authorization': 'Bearer $accessToken',
'Accept': 'application/json',
}),
HttpLoggingInterceptor()
]);
return _$ApiService(client);}
API
@Post(path: '/inspectionStore',)
@multipart
Future<Response> storeInspection(
@Part("field") String field, @PartFile("file") http.MultipartFile file);
工作代码
File actualFile = photoSection.postImage[0];
http.MultipartFile file = await http.MultipartFile.fromPath('file', actualFile.path,contentType: MediaType('image', 'jpg'));
var response = await Provider.of<ApiService>(context).storeInspection('some name',file);
我如何获得可以使用的适当数据?
我知道这有点晚了,但这可以节省一个人。因此,您需要以字符串形式提供图像的路径
@multipart
Future<Response> storeInspection(
@Part("field") String field, @PartFile("file") String file);
然后
File actualFile = photoSection.postImage[0];
var response = await Provider.of<ApiService>(context).storeInspection('some name',file.path);