【发布时间】:2021-01-21 12:02:16
【问题描述】:
我正在尝试使用 StartUpload、ContinueUpload 和 FinishUpload 函数将大文件在线上传到 SharePoint。当我使用以下代码添加文件时,这对我来说很好:
using (MemoryStream contentStream = new MemoryStream())
{
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.ContentStream = contentStream;
fileInfo.Url = uniqueFileName;
fileInfo.Overwrite = true;
uploadFile = parentFolder.Files.Add(fileInfo);
using (MemoryStream s = new MemoryStream(buffer10MB))
{
// Call the start upload method on the first slice.
bytesUploaded = uploadFile.StartUpload(uploadId, s);
ctx.ExecuteQuery();
// fileoffset is the pointer where the next slice will be added.
fileoffset = bytesUploaded.Value;
}
}
但我正在尝试使用 Files.AddUsingPath 函数而不是 Files.Add 来允许文件名中包含特殊字符。具体来说,我看到如果文件名有 % 字符,那么上面的代码将文件重命名为 %25 。但是在使用 AddUsingPath 时出现错误:
无法访问已关闭的 Stream。在 System.IO.__Error.StreamIsClosed() 在 System.IO.MemoryStream.get_Length() 在 Microsoft.SharePoint.Client.ClientRequest.WriteMimeStream(ExecuteQueryMimeInfo mimeInfo,ChunkStringBuilder sb,流 requestStream)在 Microsoft.SharePoint.Client.ClientRequest.SetupServerQuery(ChunkStringBuilder 某人)在 Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) 在 Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
AddUsingPath 的代码如下:
if(first)
using (MemoryStream contentStream = new MemoryStream())
{
FileCollectionAddParameters fileAddParameters = new FileCollectionAddParameters();
fileAddParameters.Overwrite = true;
uploadFile = parentFolder.Files.AddUsingPath(resourcePath, fileAddParameters, contentStream);
using (MemoryStream s = new MemoryStream(buffer10MB))
{
// Call the start upload method on the first slice.
bytesUploaded = uploadFile.StartUpload(uploadId, s);
ctx.ExecuteQuery();
// fileoffset is the pointer where the next slice will be added.
fileoffset = bytesUploaded.Value;
}
}
else if(continue)
{
using (MemoryStream s = new MemoryStream(buffer10MB))
{
// Continue sliced upload.
bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
ctx.ExecuteQuery(); // Get error here Cannot access a closed Stream. when continue
// Update fileoffset for the next slice.
fileoffset = bytesUploaded.Value;
}
}
我在这里所做的区别是使用 AddUsingPath 添加文件,如果它第一次上传但继续上传和完成上传功能保持不变。
如果我遗漏了什么,请告诉我。
【问题讨论】:
标签: c# sharepoint-online csom