Flutter,使用http包将图像上传到服务器

时间:2020-07-23 16:34:44

标签: flutter httprequest multipartform-data uploading

我知道这里有很多类似的问题,但是没人能解决我的问题...

我无法将服务器修改为支持base64 json格式,因此只能使用MultipartFile。

Future asyncFileUpload(File file) async {
    var request = new http.MultipartRequest("POST", Uri.parse("url..."));
    List<int> fileBytes = await file.readAsBytes();
    var multipartFile = new http.MultipartFile.fromBytes('image',fileBytes);
    request.files.add(multipartFile);
    request.headers.addAll({'Authorization': 'Bearer eyJhbGciO...'},);
    var response = await request.send();
    if (response.statusCode == 200) print('Uploaded!');
}

但是在我的应用程序逻辑中,我在http.Client的对象上有一个InterceptorContract和RetryPolicy ...

import 'package:http/http.dart' as http;

http.Client client = HttpClientWithInterceptor.build(interceptors: [
    HttpInterceptor(),
  ], retryPolicy: ExpiredTokenRetryPolicy());

class HttpInterceptor implements InterceptorContract {...}
class ExpiredTokenRetryPolicy extends RetryPolicy {...}

我确实以这种方式发布请求:

Future<bool> postNewProduct(ProductModel model) async {
    var response = await client.post("url...", body: json.encode(model));
    return response.statusCode == HttpStatus.ok;
  }
  1. 如何在客户端对象(http.Client)发布请求中集成MultipartFile?
  2. 如何将InterceptorContract和RetryPolicy设置为MultipartFile?

0 个答案:

没有答案