簡體   English   中英

上傳帶有 http.post 的圖像和 Flutter 中的注冊表單?

[英]Upload image with http.post and registration form in Flutter?

所以我想用一堆其他變量(字符串)將文件(圖像)上傳到服務器

字符串名、姓、生日、電話、地址; 文件圖片;

return http.post(
  uri,
  headers: {
    'Accept': 'application/json',
    "Authorization": "Bearer $token",
  },
  body: body,
  encoding: encoding,
);

Future<http.Response> postRegisteration() async {
    return await api.httpPost('fotApp/master', body: {
        'firstname': 'lorem',
        'lastname': 'lorem',
        'birthDay': 'lorem',
        'adresse': 'lorem',
        'phone': 'lorem',
        'image': 'lorem'
      }).then((reponse) {
        var data = jsonDecode(reponse.body);
        print(data);
    });
}

嘗試這樣的事情

在 fileList 中,您應該添加您想要上傳的任何文件

    List<MultipartFile> fileList = List();
    fileList.add(MultipartFile.fromBytes(
        'documents', await filePath.readAsBytes(),
        filename: fileName));

對於其他部分參數,請使用參數 map

    Map<String, String> params = {
      "first_name": widget.mUserDetailsInputmodel.firstName,
      "last_name": widget.mUserDetailsInputmodel.lastName,
      "email": widget.mUserDetailsInputmodel.emailAddress,
    };

然后像這樣發送請求

  Future<String> multipartRequest({var url, var partParams, var files}) async {
    Map<String, String> headers = {
      "X-API-KEY": X_API_KEY,
      "Accept": "application/json",
      "User-Auth-Token": authToken };
    var request = http.MultipartRequest("POST", Uri.parse(url));
    request.headers.addAll(headers);

    if (partParams != null) request.fields.addAll(partParams);// add part params if not null
    if (files != null) request.files.addAll(files);// add files if not null

    var response = await request.send();
    var responseData = await response.stream.toBytes();
    var responseString = String.fromCharCodes(responseData);
    print("responseBody " + responseString);
    if (response.statusCode == 200) return responseString;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM