【问题标题】:Upload large files to SharePoint Online using AddUsingPath使用 AddUsingPath 将大文件上传到 SharePoint Online
【发布时间】: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


    【解决方案1】:

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

    uploadFile = web.GetFileByServerRelativePath(resourcePath)

    如果(继续)。这对我有用。

    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

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 2014-07-01
      相关资源
      最近更新 更多