This document describes the how the FileOperationsWithActions
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 FileOperationsWithActions
project demonstrates the use of the following Dataverse Web API actions when working with file data:
- InitializeFileBlocksUpload Action
- UploadBlock Action Action
- CommitFileBlocksUpload Action
- InitializeFileBlocksDownload Action
- DownloadBlock Action
- DeleteFile Action
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 InitializeFileBlocksUploadRequest class
- Process the response with the InitializeFileBlocksUploadResponse class to get the
FileContinuationToken
that will be used in all the following requests. - Split the file up into 4MB block and send each block using the UploadBlockRequest class. Each instance must include a base64 encoded string to uniquely identify the block. This request doesn't have any response value to process.
- After all the blocks are sent, use the CommitFileBlocksUploadRequest class with an array of the base64 encoded string values to finalize the operation.
- Process the response with the CommitFileBlocksUploadResponse class to get the
FileId
value that will be used to delete the file.
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, initialize the download with the InitializeFileBlocksDownloadRequest class
- Process the response with the InitializeFileBlocksDownloadResponse class to get the
FileContinuationToken
that will be used in all the following requests. - Instantiate a
List<byte>
variable to capture the portions of the file as it is downloaded. - Download the file in 4MB blocks with multiple requests using the DownloadBlockRequest class.
- Process each response with the DownloadBlockResponse class to get the partial
byte[]
from theData
property. Add that portion to theList<byte>
. - After all the blocks are recieved, return the
List<byte>
as an array.
If the file was succesfully uploaded, use the DeleteFileRequest class to delete the file using the FileId
value returned by the CommitFileBlocksUploadResponse
. This request doesn't have any response value to process.