Flutter Upload File to Cloud Storage using GroupDocs.Parser REST API

I am uploading the file through the rest api to the parser storage but the file is corrupted during upload (ex: if original file size is 500kb then the uploaded file size is 0.23kb). Here is the code i am using.

 Future<String?> uploadDocumentToCloud(
      File? file, String? fileName, String? accessToken) async {
    var url = Uri.parse(
        "https://api.groupdocs.cloud/v1.0/parser/storage/file/$fileName");
    var body = jsonEncode({
      "file": file!.absolute.path,
    });
    var headers = {
      "accept": "application/json",
      "authorization": "Bearer $accessToken",
      "Content-Type": "multipart/form-data",
      "x-aspose-client": "Containerize.Swagger"
    };

    var response = await http.put(url, body: body, headers: headers);
    if (response.statusCode == 200) {
      var fName = jsonDecode(response.body)["uploaded"][0];
      print("fileName : $fName");
      return fName;
    } else {
      print("uploadFileError : ${response.body}");
      print('reason : ${response.reasonPhrase}');
      print(response.statusCode);
    }
    return null;
  }

  Future<String?> getParsedText(File? file, String? fileName) async {
    var accessToken = await generateJwt();
    var filePath = await uploadDocumentToCloud(file, fileName, accessToken);

    var url = Uri.parse('https://api.groupdocs.cloud/v1.0/parser/text');
    var body = jsonEncode({
      "FileInfo": {
        "FilePath": filePath,
      }
    });
    var headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'x-aspose-client': 'Containerize.Swagger',
      'Authorization': 'Bearer $accessToken',
    };
    var response = await http.post(url, body: body, headers: headers);
    if (response.statusCode == 200) {
      var text = jsonDecode(response.body)["text"];
      print("parsed Text : ${response.body}");
      return text;
    } else {
      print(response.body);
      print('reason : ${response.reasonPhrase}');
      print(response.statusCode);
    }
    return null;
  }

The upload is always successful and i can see the uploaded file in the dashboard. The parser api is also working fine but since the file is corrupted it is not able to parse it properly.

@hardhguy

Please note to upload a file to the cloud storage using GroupDocs.Parser Cloud you need to pass the file content instead of the file path. So, please update the Flutter upload file to cloud storage code accordingly. Hopefully, It will help you resolve the issue.

Flutter HTTP Upload File

curl -X PUT "https://api.groupdocs.cloud/v1.0/parser/storage/file/wavenet.pdf" 
-H "accept: application/json" 
-H "authorization: Bearer [Access_Token]" 
-H "Content-Type: multipart/form-data" 
-H "x-aspose-client: Containerize.Swagger" 
-F "file"="@C:/Temp/wavenet.pdf"

Thanks for the help its working perfectly fine now.

1 Like