将 PDF ( > 4 MB) 上传到 D365 CRM 中的文件数据类型
Upload PDF ( > 4 MB) to File datatype in D365 CRM
我正在尝试将 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);
}
UploadBlockRequest是文件上传过程中的一个环节。您需要 3 个不同的请求:
InitializeFileBlocksUploadRequest
UploadBlockRequest
CommitFileBlocksUploadRequest
UploadBlockRequest
最多可以容纳 4 MB 的数据块。您的文件最大可达 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;
}
该方法上传文件和returns文件ID。
我正在尝试将 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);
}
UploadBlockRequest是文件上传过程中的一个环节。您需要 3 个不同的请求:
InitializeFileBlocksUploadRequest
UploadBlockRequest
CommitFileBlocksUploadRequest
UploadBlockRequest
最多可以容纳 4 MB 的数据块。您的文件最大可达 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;
}
该方法上传文件和returns文件ID。