0

我正在尝试将 PDF 文件上传到 CRM 记录。我在实体中使用了一个文件类型字段来保存我上传的文件。我已经使用以下代码完成了此操作:

UploadBlockRequest blockRequest = new UploadBlockRequest();
blockRequest.BlockData = Convert.FromBase64String(documentBody);
blockRequest.BlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
blockRequest.FileContinuationToken = initResponse.FileContinuationToken;

var blockResponse = (UploadBlockResponse)service.Execute(blockRequest);

它适用于小于 4 MB 的 PDF 文件。但是,如果我尝试上传超过 4 MB 的 PDF,我会收到以下错误:

无效的文件块大小:4 MB。支持的最大块大小:4 MB。

有没有办法将大型 PDF 文件上传到 CRM 记录?

using (var stream = new MemoryStream(Convert.FromBase64String(Base64)))
                    {
                        InitializeFileBlocksUploadRequest initializeUploadRequest = new InitializeFileBlocksUploadRequest();
                        initializeUploadRequest.FileAttributeName = "my_fileTypeField";
                        initializeUploadRequest.FileName = "Test.pdf";
                        initializeUploadRequest.Target = new EntityReference("my_entity", new Guid("my_guid"));

                        var initializeUploadResponse = (InitializeFileBlocksUploadResponse)service.Execute(initializeUploadRequest);
                        var uploadRequest = new UploadBlockRequest { FileContinuationToken = initializeUploadResponse.FileContinuationToken };

                        const int blockSize = 4194304; // 4MB
                        int byteCount;
                        var blockList = new List<string>();

                        do
                        {
                            
                            //uploadRequest.BlockData = Convert.FromBase64String(documentBody);
                            byteCount = stream.Read(uploadRequest.BlockData, 0, blockSize);
                            uploadRequest.BlockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                            service.Execute(uploadRequest);
                            blockList.Add(uploadRequest.BlockId);
                            Console.WriteLine(size + " == " + blockSize);
                        } while (size == blockSize);

                        var commitRequest = new CommitFileBlocksUploadRequest
                        {
                            BlockList = blockList.ToArray(),
                            FileContinuationToken = initializeUploadResponse.FileContinuationToken,
                            FileName = initializeUploadRequest.FileName,
                            MimeType = "application/pdf"
                        };
                        var commitResponse = (CommitFileBlocksUploadResponse)service.Execute(commitRequest);
                    }
4

1 回答 1

1

UploadBlockRequest是文件上传过程中的一部分您需要 3 个不同的请求:

  1. InitializeFileBlocksUploadRequest
  2. UploadBlockRequest
  3. CommitFileBlocksUploadRequest

最多可以容纳 4 MB的UploadBlockRequest数据块。您的文件可以大到 128 MB,并且可以使用多个上传请求上传。

一个基本的上传方法可能如下所示:

private static Guid UploadFile
    (
        Stream stream,
        string fileName,
        string mimeType,
        EntityReference target,
        string fileAttributeName,
        IOrganizationService organizationService
    )
{
    var initializeUploadRequest = new InitializeFileBlocksUploadRequest
    {
        FileAttributeName = fileAttributeName,
        FileName = fileName,
        Target = target
    };

    var initializeUploadResponse = (InitializeFileBlocksUploadResponse)organizationService.Execute(initializeUploadRequest);

    const int blockSize = 4194304; // 4 MB
    int byteCount;
    var blockList = new List<string>();

    do
    {
        var buffer = new byte[blockSize];
        byteCount = stream.Read(buffer, 0, blockSize);

        if (byteCount < blockSize)
            Array.Resize(ref buffer, byteCount);

        var uploadRequest = new UploadBlockRequest
        {
            BlockData = buffer,
            BlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N"))),
            FileContinuationToken = initializeUploadResponse.FileContinuationToken
        };

        organizationService.Execute(uploadRequest);
        blockList.Add(uploadRequest.BlockId);
    } while (byteCount == blockSize);

    var commitRequest = new CommitFileBlocksUploadRequest
    {
        BlockList = blockList.ToArray(),
        FileContinuationToken = initializeUploadResponse.FileContinuationToken,
        FileName = initializeUploadRequest.FileName,
        MimeType = mimeType
    };

    var commitResponse = (CommitFileBlocksUploadResponse)organizationService.Execute(commitRequest);
    return commitResponse.FileId;
}

该方法上传文件并返回文件的ID。

于 2021-12-22T22:07:50.893 回答