微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 AddUsingPath 将大文件上传到 SharePoint Online

我正在尝试使用 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 时我得到错误

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

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

解决方法

我在另一个论坛上收到了答案。我不得不补充

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。