Save PDF files to Box cloud storage

21 Oct 20253 minutes to read

To save a PDF file to Box cloud storage using the ASP.NET MVC PDF Viewer, follow the steps below. This approach uses a server-backed web service.

Step 1: Set up a Box developer account and create a Box application

Go to the Box Developer Console, sign in or create a new account, and create a new Box application. This application provides the required credentials (Client ID and Client Secret) to authenticate and access the Box APIs. Box supports OAuth 2.0 authentication.

Step 2: Create an ASP.NET MVC PDF Viewer sample

Follow the instructions in this Getting Started guide to create a simple PDF Viewer sample in ASP.NET MVC. This sets up the basic structure of the application.

Step 3: Modify the HomeController.cs file in the project

  1. Import the required namespaces at the top of the file:
using Box.V2;
using Box.V2.Auth;
using Box.V2.Config;
using Box.V2.Models;
  1. Modify the Download() method to save the downloaded PDF files to the Box cloud storage folder.
private readonly string _clientId = "Your_Box_Storage_ClientID";
private readonly string _clientSecret = "Your_Box_Storage_ClientSecret";
private readonly string _accessToken = "Your_Box_Storage_Access_Token";
private readonly string _folderId = "Your_Folder_ID";

public async Task<ActionResult> Download(jsonObjects jsonObject)
{
    PdfRenderer pdfviewer = new PdfRenderer();
    var jsonData = JsonConverter(jsonObject);
    string documentBase = pdfviewer.GetDocumentAsBase64(jsonData);
    byte[] documentBytes = Convert.FromBase64String(documentBase.Split(',')[1]);

    string documentId = jsonData["documentId"];
    string result = Path.GetFileNameWithoutExtension(documentId);
    string fileName = result + "_downloaded.pdf";

    // Initialize the Box API client with your authentication credentials
    var auth = new OAuthSession(_accessToken, "YOUR_REFRESH_TOKEN", 3600, "bearer");
    var config = new BoxConfigBuilder(_clientId, _clientSecret, new Uri("http://boxsdk")).Build();
    var client = new BoxClient(config, auth);

    var fileRequest = new BoxFileRequest
    {
        Name = fileName,
        Parent = new BoxFolderRequest { Id = _folderId },
    };

    using (var stream = new MemoryStream(documentBytes))
    {
        var boxFile = await client.FilesManager.UploadAsync(fileRequest, stream);
    }
    return Content(documentBase);
}

NOTE

Replace the placeholders with your actual Box access token and the target folder ID. Use valid Box API credentials for Client ID and Client Secret.

Step 4: Set the PDF Viewer properties in the ASP.NET MVC PDF Viewer component

Set the documentPath property of the PDF Viewer component to the desired PDF file name that you wish to load from Box cloud storage. Ensure that the document exists in the target Box folder.

@{
    ViewBag.Title = "Home Page";
}

<div>
    <div style="height:500px;width:100%;">
        @Html.EJS().PdfViewer("pdfviewer").ServiceUrl(VirtualPathUtility.ToAbsolute("~/Home/")).DocumentPath("PDF_Succinctly.pdf").Render()
    </div>
</div>

NOTE

Install the Box.V2.Core NuGet package in the application to use the previous code example.

NOTE

Replace PDF_Succinctly.pdf with the actual document name to load from Box cloud storage. Pass the document name from the Box folder to the documentPath property of the PDF Viewer component.

View sample in GitHub