How to Upload a Single Binary File to pdfRest in .NET with C#

Learn how to use C# to upload a binary file to pdfRest using the Upload API Tool from pdfRest.
Share this page

Why Use Upload Files with C#?

The pdfRest Upload Files API Tool is a powerful resource for developers who need to upload PDF files to a server for processing. This API provides a simple and secure way to send files over the internet, ensuring that they can be easily accessed for further manipulation or storage. In this tutorial, we will demonstrate how to make an API call to the pdfRest Upload Files endpoint using C#. This is particularly useful for applications that need to handle PDF documents, such as document management systems, content management platforms, or any software that requires the processing of PDF files.

Imagine a scenario where a legal firm needs to upload and process a large number of case-related PDF documents. They could use this API to automate the upload process, saving time and reducing the risk of human error. By integrating the Upload Files API into their software, they can streamline the workflow, allowing lawyers and paralegals to focus on more critical tasks while the system takes care of the document handling.

Upload Files with C# Code Example

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "upload"))
    {
        request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var byteArray = File.ReadAllBytes("/path/to/file.pdf");
        var byteAryContent = new ByteArrayContent(byteArray);
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");

        request.Content = byteAryContent;
        var response = await httpClient.SendAsync(request);

        var apiResult = await response.Content.ReadAsStringAsync();

        Console.WriteLine("Upload response received.");
        Console.WriteLine(apiResult);
    }
}

Reference: pdfRest C# Sample Code

Breaking Down the Code

The provided C# code snippet demonstrates how to upload a file to the pdfRest API. Let's break down the key components of this code:

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })

This line initializes an instance of HttpClient with the base address set to the pdfRest API endpoint.

using (var request = new HttpRequestMessage(HttpMethod.Post, "upload"))

Here, we're creating a new HttpRequestMessage for a POST request to the "upload" endpoint.

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

The API key is added to the request headers. This key is required to authenticate the request with the pdfRest API.

var byteArray = File.ReadAllBytes("/path/to/file.pdf");
var byteAryContent = new ByteArrayContent(byteArray);

We read the file that needs to be uploaded into a byte array and then wrap it in a ByteArrayContent object.

byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
byteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");

These lines add the necessary headers to the content, indicating the type of the file and its name.

request.Content = byteAryContent;
var response = await httpClient.SendAsync(request);

The content is attached to the request, and the request is sent asynchronously. The response from the server is captured in the 'response' variable.

var apiResult = await response.Content.ReadAsStringAsync();

Finally, the response content is read as a string, which contains the result of the upload operation.

Beyond the Tutorial

By following the steps above, we've successfully uploaded a file to the pdfRest API using C#. This functionality is essential for any application that needs to process PDF files remotely. With the pdfRest API, developers can not only upload but also convert, merge, and manipulate PDF documents as needed.

Encouraged by this tutorial, you may want to explore more of what pdfRest has to offer. Feel free to demo all of the pdfRest API Tools in the API Lab. For a comprehensive understanding of the capabilities and how to use them, refer to the API Reference Guide.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.