在我的kotlin项目中,我使用retrofit,它工作得很好。
suspend fun createPlan(
context: Context?,
name: String,
file: File?
): ABC? {
val fileSignImage = file?.let {
MultipartBody.Part.createFormData(
"image",
it.getName(),
RequestBody.create("image/*".toMediaTypeOrNull(), it)
)
}
return RetrofitFactory.apiCall(context) {
RetrofitFactory.makeRetrofitService().createPlan(
name.toRequestBody("text/plain".toMediaTypeOrNull()),
fileSignImage
)
}} RetrofitService
@Multipart
@POST("create_plan")
fun createPlan(
@Part("name") name: RequestBody,
@Part image: MultipartBody.Part?
): Deferred<Response<WebApiResponse.ABCs>>如果我想使用Chopper,正确的方法是什么?
这就是我试过的
Future<Response> createPlan(
BuildContext context, String name,String path) async {
Response response;
try {
response = await _service.createPlan(
name,path);
return response;
} catch (e) {
rethrow;
}
}服务
@Post(path: "create_plan")
@multipart
Future<Response> createPlan(
@Field('name') String name,@PartFile('image') String imagePath);如何将imagePath转换为文件,以便使用Chopper将其作为文件传递给服务器
有没有人?
发布于 2020-02-07 17:32:58
我是用http而不是Chopper上传文件的。
Future<http.Response> createPlan(String name, String path) async {
var request = http.MultipartRequest(
"POST",
Uri.parse(
"http://xxx"));
request.fields['name'] = name;
request.files.add(await http.MultipartFile.fromPath(
'image',
path,
));
try {
var streamedResponse = await request.send();
var response = http.Response.fromStream(streamedResponse);
return response;
} catch (e) {
rethrow;
}
}发布于 2020-02-05 09:31:59
查看Chopper的文档,PartFile注释支持三种数据类型:
List<int>String ( file)MultipartFile的路径)(来自package:http))
您目前正在使用String,但由于未知的原因,它不适合您。第一个选项可能是最直接的,但第三个选项将是最类似于您目前在Retrofit中,所以我们可以尝试。
import 'package:http/http.dart';
...
Future<Response> createPlan(BuildContext context, String name, String path) async {
Response response;
try {
final bytes = (await File(path).readAsBytes()).toList();
final file = MultipartFile.fromBytes('image', bytes);
response = await _service.createPlan(
name,
file,
);
return response;
} catch (e) {
rethrow;
}
}服务
@Post(path: "create_plan")
@multipart
Future<Response> createPlan(
@Field('name') String name,
@PartFile('image') MultipartFile image,
);https://stackoverflow.com/questions/59988978
复制相似问题