How to Autodetect and Validate Content in C#
Content validation is an immensely important piece of any application handling file uploads. Thankfully, the code provided below makes it easy to fully validate content for dozens of common document types with built-in automatic content detection. Each file this operation is performed on will receive full content verification against its file extension, and any errors or warnings identified in that process will be quickly reported in the API response.
To use this API for free with C# code examples, start by installing the client SDK. To do so, run the following command in the Package Manager console (this installs via NuGet):
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Next, grab a free-tier API key from the Cloudmersive website and use the ready-to-run code below to structure your API call:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ValidateDocumentAutodetectValidationExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new ValidateDocumentApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
try
{
// Autodetect content type and validate
AutodetectDocumentValidationResult result = apiInstance.ValidateDocumentAutodetectValidation(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ValidateDocumentApi.ValidateDocumentAutodetectValidation: " + e.Message );
}
}
}
}
Easy! You’re all done.