다음을 통해 공유


CommitAttachmentBlocksUploadRequest Class

Definition

Contains the data needed to commit the uploaded data blocks to the attachment store.

public ref class CommitAttachmentBlocksUploadRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/crm/2011/Contracts")]
public sealed class CommitAttachmentBlocksUploadRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/crm/2011/Contracts")>]
type CommitAttachmentBlocksUploadRequest = class
    inherit OrganizationRequest
Public NotInheritable Class CommitAttachmentBlocksUploadRequest
Inherits OrganizationRequest
Inheritance
CommitAttachmentBlocksUploadRequest
Attributes

Examples

The following example shows how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance.

The following static UploadAttachment method shows how to create an attachment with a file using the InitializeAttachmentBlocksUploadRequest, UploadBlockRequest, and CommitAttachmentBlocksUploadRequest classes to return a CommitAttachmentBlocksUploadResponse with ActivityMimeAttachmentId and FileSizeInBytes properties.

/// <summary>
/// Demonstrates how to upload attachment files
/// </summary>
/// <param name="service">The authenticated IOrganizationService instance</param>
/// <param name="attachment">The attachment record.</param>
/// <param name="fileInfo">Details about the file to upload</param>
/// <param name="fileMimeType">The MIME type of the file</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
static CommitAttachmentBlocksUploadResponse UploadAttachment(
      IOrganizationService service,
      Entity attachment,
      FileInfo fileInfo,
      string fileMimeType = null)
{
      if (attachment.LogicalName != "activitymimeattachment")
      {
         throw new ArgumentException(
            "The attachment parameter must be an activitymimeattachment entity.",
            nameof(attachment));
      }

      // body value in activitymimeattachment not needed. Remove if found.
      if (attachment.Contains("body"))
      {
         attachment.Attributes.Remove("body");
      }

      // 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";
         }
      }
      // Don't overwrite mimetype value if it exists
      if (!attachment.Contains("mimetype"))
      {
         attachment["mimetype"] = fileMimeType;
      }

      // Initialize the upload
      InitializeAttachmentBlocksUploadRequest initializeRequest = new()
      {
         Target = attachment
      };

      var initializeResponse =
            (InitializeAttachmentBlocksUploadResponse)service.Execute(initializeRequest);

      string fileContinuationToken = initializeResponse.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);
      }

      // Commit the upload
      CommitAttachmentBlocksUploadRequest commitRequest = new()
      {
         BlockList = blockIds.ToArray(),
         FileContinuationToken = fileContinuationToken,
         Target = attachment
      };

      return (CommitAttachmentBlocksUploadResponse)service.Execute(commitRequest);

}

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.

For the complete sample see Sample: File operations with Attachments and Notes using the Dataverse SDK for .NET

Remarks

This request results in the uploaded data blocks being combined into a single attachment.

Constructors

CommitAttachmentBlocksUploadRequest()

Initializes a new instance of the CommitAttachmentBlocksUploadRequest class.

Properties

BlockList

Gets or sets the IDs of the uploaded data blocks, in the correct sequence, that will result in the final attachment when the data blocks are combined.

ExtensionData

Gets or sets the structure that contains extra data. Optional.

(Inherited from OrganizationRequest)
FileContinuationToken

Gets or sets a token that uniquely identifies a sequence of related data uploads.

Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
RequestId

Gets or sets the ID of the request. Optional.

(Inherited from OrganizationRequest)
RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
Target

Gets or sets the target entity.

Applies to