Assuming that developers will use several API calls, it is recommended to create a static HttpClient class which can be shared over different functions:
private static readonly HttpClient client = new HttpClient(); private static string IceViewer_url = "https://192.168.0.55 "; /*Example URL*/ private static string UploadAPI_url = IceViewer_url + "/api/dm/upload "; |
Write the web request itself:
//Uploading a document to IceViewer private async Task<string> Upload(int ContentEngine, string FilePath) { MultipartFormDataContent form = new MultipartFormDataContent();
//Parameters and values form.Add(new StringContent(ContentEngine.ToString()), "ce");
//Reading File for parameter byte[] fileContent = File.ReadAllBytes(FilePath); form.Add(new ByteArrayContent(fileContent, 0, fileContent.Length), "File", Path.GetFileName(FilePath));
//Sending the request var requestResponse = await client.PostAsync(UploadAPI_url, form); //Reading the response var responseString = await requestResponse.Content.ReadAsStringAsync(); return responseString; } |
The usage of the function and handling the response:
//1 means the file will be uploaded using the FileEngine string uploadResponse = await Upload(1, @"c:\temp\MyTestDocument.tif"); if (uploadResponse == "600") { return 0; //success } else { return 1; //upload error } |
For detailed API documentation and available parameters, please refer to the UploadFile API documentation.