Өзгерту

Бөлісу құралы:


Use file column data

File columns are different from the other system columns that can store binary data because you can't directly set the values in a create or update operation, or retrieve the file data with the record. You must use the methods described in this article to create, retrieve, update, or delete binary data for file columns.

There are several different ways to work with file column data using Web API. All methods are supported equally. Choose the method that works best for you. Because binary files may be large, it's frequently necessary to split the file into multiple chunks (or blocks) that can be sent or received sequentially or in parallel to improve performance.

File name column

Each file column has a supporting read-only string column that contains the name of the file. The schema name for this column has the same name as the file column, but has _Name appended to it. So if the schema name is sample_FileColumn, the supporting string column will be sample_FileColumn_Name. The logical name for the supporting column will be sample_filecolumn_name.

Note

The file name column does not appear in the Power Apps designer.

Relationship to FileAttachment table

When a file column is created for a table, a new one-to-many relationship is created between the table and the FileAttachment table. The name of the relationship is {table logical name}_FileAttachments. For example, if the file column is part of the account table, the relationship name will be account_FileAttachments.

You can use this relationship to return more data about the file column and any other file columns for the table. More information: Retrieve additional information about files for a record.

Behavior when retrieving

When you retrieve a record and include a file column, the value returned will be a unique identifier for the file. You can use this value to delete the file using the DeleteFile message. There's no other use for this ID other than to check whether the column has a value. More information: Use the DeleteFile message.

The following examples show what you can expect when retrieving data from file columns as you would with other columns.

These examples retrieve the name, sample_filecolumn, and sample_filecolumn_name columns for an account record:

static void RetrieveAccountRecordWithFileColumns(
    IOrganizationService service,
    Guid accountid) 
{

   Entity account = service.Retrieve(
         "account", 
         accountid, 
         new ColumnSet("name", "sample_filecolumn", "sample_filecolumn_name"));

   Console.WriteLine($"name: {account["name"]}");
   Console.WriteLine($"sample_filecolumn: {account["sample_filecolumn"]}");
   Console.WriteLine($"sample_filecolumn_name: {account["sample_filecolumn_name"]}");
}

Output:

name: Contoso Ltd.
sample_filecolumn: <file id>
sample_filecolumn_name: 25mb.pdf

More information:

Retrieve additional information about files for a record

You can use the relationship between the file column table and the FileAttachment table to return information about all file columns associated to that table row.

The RetrieveAccountRecordWithFileData static method shows how to return information about all the file columns that contain data related to the account record with the matching accountid value.

static void RetrieveAccountRecordWithFileData(
    IOrganizationService service, 
    Guid accountid)
{
    // Create query for related records
    var relationshipQueryCollection = new RelationshipQueryCollection {
        {
            new Relationship("account_FileAttachments"),
            new QueryExpression("fileattachment"){
                ColumnSet = new ColumnSet(
                                "createdon",
                                "mimetype",
                                "filesizeinbytes",
                                "filename",
                                "regardingfieldname",
                                "fileattachmentid")
            }
        }
    };

    // Include the related query with the Retrieve Request
    RetrieveRequest request = new RetrieveRequest
    {
        ColumnSet = new ColumnSet("accountid"),
        RelatedEntitiesQuery = relationshipQueryCollection,
        Target = new EntityReference("account", accountid)
    };

    // Send the request
    RetrieveResponse response = (RetrieveResponse)service.Execute(request);

    //Display related FileAttachment data for the account record
    response.Entity.RelatedEntities[new Relationship("account_FileAttachments")]
        .Entities.ToList().ForEach(e =>
        {
            Console.WriteLine($"createdon: {e.FormattedValues["createdon"]}");
            Console.WriteLine($"mimetype: {e["mimetype"]}");
            Console.WriteLine($"filesizeinbytes: {e.FormattedValues["filesizeinbytes"]}");
            Console.WriteLine($"filename: {e["filename"]}");
            Console.WriteLine($"regardingfieldname: {e["regardingfieldname"]}");
            Console.WriteLine($"fileattachmentid: {e["fileattachmentid"]}");
        });
}

Output:

In this case, there's a single file column in the account table named sample_filecolumn, and this is the data about the file stored in that column.

createdon: 10/22/2022 2:01 PM
mimetype: application/pdf
filesizeinbytes: 25,870,370
filename: 25mb.pdf
regardingfieldname: sample_filecolumn
fileattachmentid: 63a6afb7-4c52-ed11-bba1-000d3a9933c9

More information:

Upload Files

There are three different ways to upload files to a file column:

  • Use Dataverse messages available to both the SDK and Web API
  • Upload a file in a single request using Web API
  • Upload the file in chunks using Web API

Note

  • You should verify whether the column maximum file size is large enough to accept the file you are uploading. More information: Check maximum file size.
  • You can also use these APIs to upload image column data. More information: Use image column data

Use Dataverse messages to upload a file

You can use Dataverse messages using the SDK for .NET or Web API. Uploading a file this way requires using a set of three messages:

Message Description
InitializeFileBlocksUpload Use this message to indicate the column that you want to upload a file to. It returns a file continuation token that you can use to upload the file in blocks using the UploadBlock message and with CommitFileBlocksUpload.
UploadBlock Split your file into blocks and generate a blockid for each block. Then make multiple requests until all the blocks have been sent together with the file continuation token.
CommitFileBlocksUpload After you have sent requests for all the blocks using UploadBlock, use this message to commit the upload operation by sending:
- The list of generated blockids
- The name of the file
- The MIME type of the file
- The file continuation token

You can use a function like the following to upload a file or image using the InitializeFileBlocksUploadRequest, UploadBlockRequest, and CommitFileBlocksUploadRequest classes.

/// <summary>
/// Uploads a file or image column value
/// </summary>
/// <param name="service">The service</param>
/// <param name="entityReference">A reference to the record with the file or image column</param>
/// <param name="fileAttributeName">The name of the file or image column</param>
/// <param name="fileInfo">Information about the file or image to upload.</param>
/// <param name="fileMimeType">The mime type of the file or image, if known.</param>
/// <returns></returns>
static Guid UploadFile(
         IOrganizationService service,
         EntityReference entityReference,
         string fileAttributeName,
         FileInfo fileInfo,
         string fileMimeType = null)
{

   // Initialize the upload
   InitializeFileBlocksUploadRequest initializeFileBlocksUploadRequest = new()
   {
         Target = entityReference,
         FileAttributeName = fileAttributeName,
         FileName = fileInfo.Name
   };

   var initializeFileBlocksUploadResponse =
         (InitializeFileBlocksUploadResponse)service.Execute(initializeFileBlocksUploadRequest);

   string fileContinuationToken = initializeFileBlocksUploadResponse.FileContinuationToken;

   // Capture blockids while uploading
   List<string> blockIds = new();

   using Stream uploadFileStream = fileInfo.OpenRead();

   int blockSize = 4 * 1024 * 1024; // 4 MB

   byte[] buffer = new byte[blockSize];
   int bytesRead = 0;

   long fileSize = fileInfo.Length;
   // The number of iterations that will be required:
   // int blocksCount = (int)Math.Ceiling(fileSize / (float)blockSize);
   int blockNumber = 0;

   // While there is unread data from the file
   while ((bytesRead = uploadFileStream.Read(buffer, 0, buffer.Length)) > 0)
   {
         // The file or final block may be smaller than 4MB
         if (bytesRead < buffer.Length)
         {
            Array.Resize(ref buffer, bytesRead);
         }

         blockNumber++;

         string blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));

         blockIds.Add(blockId);

         // Prepare the request
         UploadBlockRequest uploadBlockRequest = new()
         {
            BlockData = buffer,
            BlockId = blockId,
            FileContinuationToken = fileContinuationToken,
         };

         // Send the request
         service.Execute(uploadBlockRequest);
   }

   // Try to get the mimetype if not provided.
   if (string.IsNullOrEmpty(fileMimeType))
   {
         var provider = new FileExtensionContentTypeProvider();

         if (!provider.TryGetContentType(fileInfo.Name, out fileMimeType))
         {
            fileMimeType = "application/octet-stream";
         }
   }

   // Commit the upload
   CommitFileBlocksUploadRequest commitFileBlocksUploadRequest = new()
   {
         BlockList = blockIds.ToArray(),
         FileContinuationToken = fileContinuationToken,
         FileName = fileInfo.Name,
         MimeType = fileMimeType
   };

   var commitFileBlocksUploadResponse =
         (CommitFileBlocksUploadResponse)service.Execute(commitFileBlocksUploadRequest);

   return commitFileBlocksUploadResponse.FileId;

}

More information:

Note

This example method includes some logic to try to get the MIME type of the file using the FileExtensionContentTypeProvider.TryGetContentType(String, String) method if it isn't provided. If the type isn't found, it's set to application/octet-stream.

Upload a file in a single request using Web API

If the size of the file is less than 128 MB, you can upload the file in a single request using the Web API.

The following example uploads a text file named 4094kb.txt to the file column named sample_filecolumn on the account table for a record with accountid equal to <accountid>.

Request:

PATCH [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Accept: application/json
Content-Type: application/octet-stream
x-ms-file-name: 4094kb.txt
Content-Length: 4191273

< binary content removed for brevity>

Response:

HTTP/1.1 204 NoContent
OData-Version: 4.0

PowerShell example to upload file in a single request

The following PowerShell Set-FileColumn function demonstrates how to upload a file in a single request using Web API.

Learn more about using PowerShell and Visual Studio Code with the Dataverse Web API:

This function requires a connection using that sets the global $baseURI and $baseHeaders that are set using the Connect function described in Create a Connect function.

<#
.SYNOPSIS
Sets a column value for a file in a specified table.

.DESCRIPTION
The Set-FileColumn function sets a column value for a file in a specified table. 
It uses a single request and can work with files less than 128 MB.

.PARAMETER setName
The entity set name of the table where the file is stored.

.PARAMETER id
The unique identifier of record.

.PARAMETER columnName
The logical name of the file column to set the value for.

.PARAMETER file
The path to the file to upload.

.EXAMPLE
Set-FileColumn `
   -setName 'accounts' `
   -id [System.Guid]::New('12345678-1234-1234-1234-1234567890AB') `
   -columnName 'new_filecolumn' `
   -file 'C:\Path\To\File.txt'
   
Sets the value of the 'new_filecolumn' column for the file with the specified ID 
in the account table to the contents of the File.txt file.

#>
function Set-FileColumn {
   param (
      [Parameter(Mandatory)]
      [string]
      $setName,
      [Parameter(Mandatory)]
      [System.Guid]
      $id,
      [Parameter(Mandatory)]
      [string]
      $columnName,
      [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
      [System.IO.FileInfo]$file
   )

   $uri = '{0}{1}({2})/{3}' -f $baseURI, $setName, $id, $columnName

   $patchHeaders = $baseHeaders.Clone()
   $patchHeaders.Add('Content-Type', 'application/octet-stream')
   $patchHeaders.Add('x-ms-file-name', $file.Name)

   $body = [System.IO.File]::ReadAllBytes($file.FullName)

   $FileUploadRequest = @{
      Uri     = $uri
      Method  = 'Patch'
      Headers = $patchHeaders
      Body    = $body
   }

   Invoke-RestMethod @FileUploadRequest
}

Upload the file in chunks using Web API

To upload your file in chunks using the Web API, use the following set of requests.

The following example uploads a PDF file named 25mb.pdf to the file column named sample_filecolumn on the account table for a record with accountid equal to <accountid>.

Request:

The first request must include this header:x-ms-transfer-mode: chunked

Set the file name using the x-ms-file-name query parameter.

PATCH [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn?x-ms-file-name=25mb.pdf HTTP/1.1
x-ms-transfer-mode: chunked
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Accept: application/json

Note

The file name can also be included as a x-ms-file-name request header, but this will not support file names outside the ASCII character set. If the header is used, it will take precedence over the x-ms-file-name query parameter.

Response:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Location: [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn?sessiontoken=<sessiontoken value removed for brevity>
OData-Version: 4.0
x-ms-chunk-size: 4194304
Access-Control-Expose-Headers: x-ms-chunk-size

The response Location header includes a URL to use in subsequent requests. It includes a sessiontoken query parameter that indicates that all the requests sent using it are part of the same operation.

The response includes the following headers.

Header Description
x-ms-chunk-size Provides a recommended chunk size in bytes.
Accept-Ranges Indicates the server supports partial requests from the client for file downloads. The value bytes indicates that the range value in subsequent requests should be in bytes.
Access-Control-Expose-Headers Indicates that the x-ms-chunk-size header value should be made available to scripts running in the browser, in response to a cross-origin request.

Request:

Subsequent requests should use the value of the Location header returned by the first request so that the sessiontoken value is included.

Each request must contain that portion of the file in the body and the following headers:

Header Description
x-ms-file-name The name of the file.
Content-Type Set to application/octet-stream
Content-Range Using this format:
<unit> <range-start>-<range-end>/<size>
The value of the first request: bytes 0-4194303/25870370 indicates that the measurement is using bytes. This request includes the first 4194303 bytes of a file that is 25870370 bytes (almost 25 MB) in size.
Each subsequent request will see this value increase until the entire file has been sent:
bytes 4194304-8388607/25870370
bytes 8388608-12582911/25870370
bytes 12582912-16777215/25870370
bytes 16777216-20971519/25870370
bytes 20971520-25165823/25870370
bytes 25165824-25870369/25870370
Content-Length Indicates the size of the message. In the example above, this value for the last request will be 704546 rather than 4194304.
PATCH [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn?sessiontoken=<sessiontoken value removed for brevity> HTTP/1.1
x-ms-file-name: 25mb.pdf
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Accept: application/json
Content-Type: application/octet-stream
Content-Range: bytes 0-4194303/25870370
Content-Length: 4194304

< byte[] content removed for brevity>

For each request that contains partial content, the response will be 206 PartialContent.

Response:

HTTP/1.1 206 PartialContent
OData-Version: 4.0

For the final request that includes the last chunk of the file, the response will be 204 NoContent.

Response:

HTTP/1.1 204 NoContent
OData-Version: 4.0

PowerShell example to upload file in chunks

The following PowerShell Set-FileColumnInChunks function demonstrates how to upload a file in chunks. This function requires a connection using that sets the global $baseURI and $baseHeaders that are set using the Connect function described in Create a Connect function.

<#
.SYNOPSIS
Sets a column value for a file in a specified table.

.DESCRIPTION
The Set-FileColumnInChunks function sets a column value for a file in a specified table. 
It uses chunked file upload to efficiently upload large files.

.PARAMETER setName
The name of the table where the file is stored.

.PARAMETER id
The unique identifier of record.

.PARAMETER columnName
The logical name of the file column to set the value for.

.PARAMETER file
The path to the file to upload.

.EXAMPLE
Set-FileColumnInChunks `
   -setName 'accounts' `
   -id [System.Guid]::New('12345678-1234-1234-1234-1234567890AB') `
   -columnName 'new_filecolumn' `
   -file 'C:\Path\To\File.txt'
   
Sets the value of the 'new_filecolumn' column for the file with the specified ID in the account table to the contents of the File.txt file.

#>
function Set-FileColumnInChunks {
   param (
      [Parameter(Mandatory)]
      [string]
      $setName,
      [Parameter(Mandatory)]
      [System.Guid]
      $id,
      [Parameter(Mandatory)]
      [string]
      $columnName,
      [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
      [System.IO.FileInfo]$file
   )

   $uri = '{0}{1}({2})' -f $baseURI, $setName, $id
   $uri += '/{0}?x-ms-file-name={1}' -f $columnName, $file.Name

   $chunkHeaders = $baseHeaders.Clone()
   $chunkHeaders.Add('x-ms-transfer-mode', 'chunked')

   $InitializeChunkedFileUploadRequest = @{
      Uri     = $uri
      Method  = 'Patch'
      Headers = $chunkHeaders
   }

   Invoke-RestMethod @InitializeChunkedFileUploadRequest `
      -ResponseHeadersVariable rhv

   $locationUri = $rhv['Location'][0]
   $chunkSize = [int]$rhv['x-ms-chunk-size'][0]

   $bytes = [System.IO.File]::ReadAllBytes($file.FullName)

   for ($offset = 0; $offset -lt $bytes.Length; $offset += $chunkSize) {
         
      $count = if (($offSet + $chunkSize) -gt $bytes.Length) 
                  { $bytes.Length % $chunkSize } 
               else { $chunkSize }
      
      $lastByte = $offset + ($count - 1)

      $range = 'bytes {0}-{1}/{2}' -f $offset, $lastByte, $bytes.Length

      $contentHeaders = $baseHeaders.Clone()
      $contentHeaders.Add('Content-Range', $range)
      $contentHeaders.Add('Content-Type', 'application/octet-stream')
      $contentHeaders.Add('x-ms-file-name', $file.Name)

      $UploadFileChunkRequest = @{
         Uri     = $locationUri
         Method  = 'Patch'
         Headers = $contentHeaders
         Body    = [byte[]]$bytes[$offSet..$lastByte]
      }

      Invoke-RestMethod @UploadFileChunkRequest
   }
}

Check maximum file size

Before you upload a file, you can check whether the size of the file exceeds the configured Maximum file size stored in the MaxSizeInKB property.

If you try to upload a file that is too large, you'll get the following error:

Name: unManagedidsattachmentinvalidfilesize
Code: 0x80044a02
Number: -2147202558
Message: Attachment file size is too big.

You can use the following examples to check the maximum file size:

The static GetFileColumnMaxSizeInKb method returns the MaxSizeInKB value for a file column.

/// <summary>
/// Retrieves the MaxSizeInKb property of a file column.
/// </summary>
/// <param name="service">IOrganizationService</param>
/// <param name="entityLogicalName">The logical name of the table that has the column</param>
/// <param name="fileColumnLogicalName">The logical name of the file column.</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static int GetFileColumnMaxSizeInKb(
    IOrganizationService service, 
    string entityLogicalName, 
    string fileColumnLogicalName) 
{

   RetrieveAttributeRequest retrieveAttributeRequest = new() { 
         EntityLogicalName = entityLogicalName,
         LogicalName = fileColumnLogicalName
   };

   RetrieveAttributeResponse retrieveAttributeResponse;
   try
   {
         retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
   }
   catch (Exception)
   {
         throw;
   }

   if (retrieveAttributeResponse.AttributeMetadata is FileAttributeMetadata fileColumn)
   {
         return fileColumn.MaxSizeInKB.Value;
   }
   else
   {
         throw new Exception($"{entityLogicalName}.{fileColumnLogicalName} is not a file column.");
   }
}

More information:

Download Files

There are three different methods to download files from a file column:

  • Use Dataverse messages available to both the SDK and Web API
  • Download a file in a single request using Web API
  • Download the file in chunks using Web API

Note

These methods can also be used to download image columns, but there are some differences. More information: Download images

For on-premises environments or when an environment uses the Self-managed key (BYOK), the file is not in file storage. When a file is not in file storage, downloading in multiple chunks is not supported. The InitializeFileBlocksDownloadResponse ComplexType and InitializeFileBlocksDownloadResponse Class have an IsChunkingSupportedproperty that indicates if the file can be downloaded in multiple chunks or not. If chunking is not supported, then set BlockLength to the file size.

Trying to download partial chunk when IsChunkingSupported is set to false will result in this error:

Name: UploadingAndDownloadingInMultipleChunksNotSupported
Code: 0x80090017
Message: Downloading in multiple chunks is not supported for the files stored in the database.

Use Dataverse messages to download a file

You can use Dataverse messages using the SDK for .NET or Web API. Downloading a file this way requires using a pair of messages:

Message Description
InitializeFileBlocksDownload Use this message to indicate the column that you want to download a file from. It returns the file size in bytes and a file continuation token that you can use to download the file in blocks using the DownloadBlock message.
DownloadBlock Request the size of the block, the offset value and the file continuation token.

Once you've downloaded all the blocks, you must join them to create the entire downloaded file.

You can use a function like the following to download a file or image using the SDK using the InitializeFileBlocksDownloadRequest and DownloadBlockRequest classes.

/// <summary>
/// Downloads a file or image
/// </summary>
/// <param name="service">The service</param>
/// <param name="entityReference">A reference to the record with the file or image column</param>
/// <param name="attributeName">The name of the file or image column</param>
/// <returns></returns>
private static byte[] DownloadFile(
            IOrganizationService service,
            EntityReference entityReference,
            string attributeName)
{
   InitializeFileBlocksDownloadRequest initializeFileBlocksDownloadRequest = new()
   {
         Target = entityReference,
         FileAttributeName = attributeName
   };

   var initializeFileBlocksDownloadResponse =
         (InitializeFileBlocksDownloadResponse)service.Execute(initializeFileBlocksDownloadRequest);

   string fileContinuationToken = initializeFileBlocksDownloadResponse.FileContinuationToken;
   long fileSizeInBytes = initializeFileBlocksDownloadResponse.FileSizeInBytes;

   List<byte> fileBytes = new((int)fileSizeInBytes);

   long offset = 0;
   // If chunking is not supported, chunk size will be full size of the file.
   long blockSizeDownload = !initializeFileBlocksDownloadResponse.IsChunkingSupported ? fileSizeInBytes :  4 * 1024 * 1024;

   // File size may be smaller than defined block size
   if (fileSizeInBytes < blockSizeDownload)
   {
         blockSizeDownload = fileSizeInBytes;
   }

   while (fileSizeInBytes > 0)
   {
         // Prepare the request
         DownloadBlockRequest downLoadBlockRequest = new()
         {
            BlockLength = blockSizeDownload,
            FileContinuationToken = fileContinuationToken,
            Offset = offset
         };

         // Send the request
         var downloadBlockResponse =
                  (DownloadBlockResponse)service.Execute(downLoadBlockRequest);

         // Add the block returned to the list
         fileBytes.AddRange(downloadBlockResponse.Data);

         // Subtract the amount downloaded,
         // which may make fileSizeInBytes < 0 and indicate
         // no further blocks to download
         fileSizeInBytes -= (int)blockSizeDownload;
         // Increment the offset to start at the beginning of the next block.
         offset += blockSizeDownload;
   }

   return fileBytes.ToArray();
}

More information:

Download a file in a single request using Web API

The following example downloads a text file named 4094kb.txt from the file column named sample_filecolumn on the account table for a record with accountid equal to <accountid>.

Request:

GET [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn/$value HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Accept: application/json

Response:

The response includes the following headers.

Header Description
x-ms-file-size The size of the file in bytes.
x-ms-file-name The name of the file.
mimetype The MIME type of the file.
Access-Control-Expose-Headers Indicates that the x-ms-file-size, x-ms-file-name, and mimetype header values should be made available to scripts running in the browser, in response to a cross-origin request.
HTTP/1.1 200 OK
x-ms-file-size: 4191273
x-ms-file-name: 4094kb.txt
mimetype: text/plain
Access-Control-Expose-Headers: x-ms-file-size; x-ms-file-name; mimetype

< byte[] content removed for brevity. >

Download the file in chunks using Web API

Note

The example below is for cases when IsChunkingSupported is set to true. If it is false, please use Download a file in a single request using Web API.

To download your file in chunks using the Web API, use the following set of requests.

The following example downloads a PDF file named 25mb.pdf to the file column named sample_filecolumn on the account table for a record with accountid equal to <accountid>.

Request:

Use the Range header to specify the number of bytes to return using this format:
<unit>=<range-start>-<range-end>
Where the unit is bytes and the range-start for the first request is 0.

You won't know how many iterations are required to download the entire file until you get the first response where the x-ms-file-size response header tells you the size of the file.

While the <range-start> is smaller than total size of the file, for each subsequent request increment the <range-start> and <range-end> values to request the next chunk of the file.

GET [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn/$value HTTP/1.1
Range: bytes=0-4194303
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Accept: application/json

Response:

The response includes the following headers:

Header Description
x-ms-file-size The size of the file in bytes.
x-ms-file-name The name of the file.
x-ms-chunk-size Provides a recommended chunk size in bytes.
mimetype The MIME type of the file.
Access-Control-Expose-Headers Indicates that the x-ms-file-size, x-ms-file-name, x-ms-chunk-size, and mimetype header values should be made available to scripts running in the browser, in response to a cross-origin request.
HTTP/1.1 206 PartialContent
x-ms-file-size: 25870370
x-ms-file-name: 25mb.pdf
x-ms-chunk-size: 4194304
mimetype: application/pdf
Access-Control-Expose-Headers: x-ms-file-size; x-ms-file-name; x-ms-chunk-size; mimetype
OData-Version: 4.0

< byte[] content removed for brevity. >

Delete Files

There are two different ways to delete files to a file column:

  • Use the Dataverse DeleteFile message available to both the SDK and Web API
  • Send a DELETE request using Web API to the file column of the record.

Use the DeleteFile message

Using the unique identifier returned from the CommitFileBlocksUploadResponse.FileId or retrieved from the column as described in Behavior when retrieving, you can delete the file using the DeleteFile message.

You can use a function like the following to delete a file using the unique identifier using the DeleteFileRequest Class.

static Guid DeleteFile(IOrganizationService service, Guid fileId)
{
   DeleteFileRequest deleteFileRequest = new()
   {
      FileId = fileId
   };
   service.Execute(deleteFileRequest);
}

Send DELETE request to the file column

With the Web API, you can delete a file by sending a DELETE request to the location of the file resource.

The following example deletes file data for a column named sample_filecolumn on the account table for a record with accountid equal to <accountid>.

Request:

DELETE [Organization Uri]/api/data/v9.2/accounts(<accountid>)/sample_filecolumn HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
If-None-Match: null
Accept: application/json

Response:

HTTP/1.1 204 NoContent
OData-Version: 4.0

More information: Delete a single property value

See Also

Files and images overview
Column data types > File columns
Work with file column definitions using code
Sample: File Operations using Dataverse SDK for .NET
Sample: File Operations using Dataverse Web API