I want to Convert Doc file into HTML and i try this :
Future getAccessToken() async {
final tokenUrl = ‘https://api.aspose.cloud/connect/token’;
final response = await http.post(
Uri.parse(tokenUrl),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
'grant_type': 'client_credentials',
'client_id': '399b4908-81fa-4cf0-bace-2c1d5a9bf699',
'client_secret': '6e9c532165878e6bae767419ce02536b',
},
);
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
return responseData['access_token'];
} else {
throw Exception('Failed to obtain access token: ${response.body}');
}
}
Future uploadFileToAspose(File docFile) async {
final apiUrl = ‘https://api.aspose.cloud/v4.0/words/convert’;
final accessToken = await getAccessToken();
final uri = Uri.parse(apiUrl);
final request = http.MultipartRequest('POST', uri)
..headers['Authorization'] = 'Bearer $accessToken'
..files.add(await http.MultipartFile.fromPath('file', docFile.path));
final response = await request.send();
if (response.statusCode == 200) {
final responseData = await http.Response.fromStream(response);
final jsonResponse = jsonDecode(responseData.body);
return jsonResponse['url']; // URL of the converted file
} else {
final responseData = await http.Response.fromStream(response);
throw Exception('File upload failed: ${responseData.body}');
}
}
Future convertAndDisplayDoc() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
File docFile = await getLocalFile('sample.docx'); // Replace with your DOC file
String viewUrl = await uploadFileToAspose(docFile);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DocViewerScreen(url: viewUrl),
),
);
} catch (error) {
setState(() {
_error = error.toString();
});
} finally {
setState(() {
_isLoading = false;
});
}
}
but its give me error File upload failed