This document describes the how the FileOperationsWithChunks
project implements the common operations described in Web API File Operations sample. See that document for an overview and how to run this sample.
The FileOperationsWithChunks
project demonstrates how to work with files using standard Http methods and the Dataverse Web API.
This sample uses classes defined within the WebAPIService project Messages folder so that they can be re-used. These classes inherit from the .NET HttpRequestMessage and HttpResponseMessage classes. These classes have constructors that accept the relevant variables to compose the requests to send, or to deserialize the responses into standard properties.
The code for this sample is in the Program.cs file.
To upload a PDF file named 25MB.pdf
to the sample_FileColumn
file column on the account record, this sample uses an UploadFile
static method that accepts all the parameters needed to make the following requests:
- Initialize the upload with the InitializeChunkedFileUploadRequest class
- Process the response with the InitializeChunkedFileUploadResponse class to get the URL from the
Location
header to send subsequent requests. - Split the file up into 4MB block and send each block using the UploadFileChunkRequest class. This request doesn't have any response value to process.
To download the PDF file named 25MB.pdf
that was just uploaded to the sample_FileColumn
file column on the account record, this sample uses an DownloadFile
static method that accepts all the parameters needed to make the following requests:
- If the file was succesfully uploaded, instantiate a
byte[]
variable to capture the contents of the file. - Send a series of DownloadFileChunkRequest class while there are chunks remaining to download.
- Process each response with the DownloadFileChunkResponse class to get
byte[]
data passed in the content of the response. This response also extracts the size of the file from thex-ms-file-size
so the total number of iterations required can be known. Copy the data from each response into thebyte[]
variable. - After all the blocks are recieved, return the
byte[]
variable.
If the file was succesfully uploaded, use the DeleteColumnValueRequest class to delete the file.This method can be used to delete the value of any type of column. This request doesn't have any response value to process.