How to Upload Multiple Files to pdfRest in .NET with C#

Learn how to Upload Multiple Files to pdfRest in .NET with C# by calling Upload Files API Tool by pdfRest.
Share this page

Why Use Upload Files with C#?

The pdfRest Upload Files API Tool is designed to allow users to upload documents to the pdfRest cloud for processing. This tutorial will demonstrate how to send an API call to the Upload Files endpoint using C#. In real-world scenarios, developers might need to upload PDF files for conversion, merging, splitting, or other document processing tasks. For instance, a user might want to automate the process of uploading financial reports to a cloud service for archival or further processing.

Upload Files with C# Code Example

using System.Text;

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", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        request.Headers.Accept.Add(new("application/json"));
        var multipartContent = new MultipartFormDataContent();

        var byteArray = File.ReadAllBytes("/path/to/file");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");


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

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

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

Source: pdf-rest-api-samples

Breaking Down the Code

To understand how the code works, let's break it down into smaller snippets:

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

This creates an instance of HttpClient with the base address set to the pdfRest API.

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

This line adds your API key to the request headers to authenticate your API call.

var multipartContent = new MultipartFormDataContent();

A MultipartFormDataContent object is created to hold the file content for upload.

var byteArray = File.ReadAllBytes("/path/to/file");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name");

Here, the file is read into a byte array, wrapped in a ByteArrayContent, and added to the multipart content with the name "file". Replace "/path/to/file" and "file_name" with the actual file path and desired file name.

byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

The content type for the file is set to "application/pdf" indicating that the file is a PDF document.

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

Finally, the request is sent asynchronously, and the response from the server is read as a string.

Beyond the Tutorial

By following the steps above, you've learned how to upload a file to pdfRest using C#. This enables you to integrate file uploads into your applications, automating workflows that require PDF processing in the cloud. You can now explore other pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for further details on available endpoints and their functionalities.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/pdf-rest-api-samples/tree/main/DotNET/Endpoint%20Examples/JSON%20Payload/upload.cs.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us