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

Upload large files to SharePoint Online using AddUsingPath

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

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 时出现错误:

Cannot access a closed Stream. at System.IO.__Error.StreamIsClosed() at System.IO.MemoryStream.get_Length() at Microsoft.SharePoint.Client.ClientRequest.WriteMimeStream(ExecuteQueryMimeInfo mimeInfo, ChunkStringBuilder sb, Stream requestStream) at Microsoft.SharePoint.Client.ClientRequest.SetupServerQuery(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) at 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 添加文件,如果它的第一次上传但继续上传和完成上传功能保持不变。

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

我在另一个论坛上收到了答案。我必须添加

uploadFile = web.GetFileByServerRelativePath(resourcePath)

在 if(continue) 内。这对我有用。

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://docs.microsoft.com/en-us/answers/questions/244117/upload-large-files-to-sharepoint-online-using-addu.html