我正在尝试将 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);
}