使用 AddUsingPath 将大型文件上传到 SharePoint Online

Jiale Xue - MSFT 37,136 信誉分 Microsoft 供应商
2024-03-29T06:15:55.7133333+00:00

我正在尝试使用 StartUpload、ContinueUpload 和 FinishUpload 函数将大文件在线上传到 SharePoint。当我使用以下代码添加文件时,这对我来说很好用:

using (MemoryStream contentStream = new MemoryStream()) { FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.ContentStream = 内容流; fileInfo.Url = uniqueFileName; fileInfo.Overwrite = true; uploadFile = parentFolder.Files.Add(文件信息); using (MemoryStream s = new MemoryStream(buffer10MB)) { // 调用第一个切片的开始上传方法。 bytesUploaded = uploadFile.StartUpload(uploadId, s); CTX的。执行查询(); // fileoffset 是将添加下一个切片的指针。 文件偏移量 = bytesUploaded.Value; } }

但是我正在尝试使用Files.AddUsingPath函数而不是Files.Add来允许文件名中的特殊字符。具体来说,我看到如果文件名具有 % 字符,那么上面的代码会将文件重命名为 %25 。但是在使用 AddUsingPath 时,我收到错误:

无法访问已关闭的流。以及 System.IO.__Error.StreamIsClosed() 和 System.IO.MemoryStream.get_Length() 和 Microsoft.SharePoint.Client.ClientRequest.WriteMimeStream(ExecuteQueryMimeInfo mimeInfo, ChunkStringBuilder sb, Stream requestStream) 和 Microsoft.SharePoint.Client.ClientRequest.SetupServerQuery(ChunkStringBuilder sb) 和 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)) { // 调用第一个切片的开始上传方法。 bytesUploaded = uploadFile.StartUpload(uploadId, s); CTX的。执行查询(); // fileoffset 是将添加下一个切片的指针。 文件偏移量 = bytesUploaded.Value; } } else if(continue) { using (MemoryStream s = new MemoryStream(buffer10MB)) { // 继续切片上传。 bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s); CTX的。执行查询();在此处出现错误:无法访问已关闭的流。when continue // 更新下一个切片的 fileoffset。 文件偏移量 = bytesUploaded.Value; } }

我在这里所做的区别是,如果文件的第一次上传,但继续上传和完成上传功能保持不变,则使用 AddUsingPath 添加文件。

如果我遗漏了什么,请告诉我。

Note:此问题总结整理于: Upload large files to SharePoint Online using AddUsingPath

SharePoint
SharePoint
一组用于共享和管理内容、知识和应用程序的 Microsoft 产品和技术。
5 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
140 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 44,186 信誉分 Microsoft 供应商
    2024-03-29T07:39:38.6866667+00:00

    您需要在以下标题下添加:uploadFile = web.GetFileByServerRelativePath(resourcePath);else if(continue) C#

    else if(continue)  
    {  
    	uploadFile = web.GetFileByServerRelativePath(resourcePath)  
    	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;  
    	}  
    }  
    

    我给你的演示:https://github.com/hb1546297019/michael/blob/master/uploadLargeFilesCSOM.cs


    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助