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

前端 未结 1 1720
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 01:59

so i want to upload a File (image) to a server with a bunch of other variable (Strings)

String firstname , lastname , birthDay, phone , adresse ; File image;

相关标签:
1条回答
  • 2021-01-15 02:34

    Try something like this

    In fileList, you should add any file you like to upload

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

    For other part parameters use params map

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

    Then send request somthing like this

      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;
      }
    
    
    0 讨论(0)
提交回复
热议问题