SharePoint Online,使用 SharePointClient 上传大于 2mb 的文件

SharePoint Online, uploading files larger than 2mb using SharePointClient

尝试通过 SharePointClient upload 远程将文件上传到 SharePoint online 时,我遇到了 2mb 的文件大小限制。根据我的搜索,人们似乎已经使用 PowerShell 克服了这个限制,但是有没有办法使用 .Net C# 中的本机 SharePointClient 包来克服这个限制?这是我现有的代码示例:

    using (var ctx = new Microsoft.SharePoint.Client.ClientContext(httpUrl))
    {
        ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, passWord);
        try
        {
            string uploadFilename = string.Format(@"{0}.{1}", string.IsNullOrWhiteSpace(filename) ? submissionId : filename, formatExtension);
            logger.Info(string.Format("SharePoint uploading: {0}", uploadFilename));
            new SharePointClient().Upload(ctx, sharePointDirectoryPath, uploadFilename, formatData);
        }
    }

我从以下网站了解到您可以使用 ContentStream,只是不确定它如何映射到 SharePointClient(如果有的话):

https://msdn.microsoft.com/en-us/pnp_articles/upload-large-files-sample-app-for-sharepoint

更新:

根据我现在的建议解决方案:

public void UploadDocumentContentStream(ClientContext ctx, string libraryName, string filePath)
{

    Web web = ctx.Web;

    using (FileStream fs = new FileStream(filePath, FileMode.Open))
    {
        FileCreationInformation flciNewFile = new FileCreationInformation();

        // This is the key difference for the first case - using ContentStream property
        flciNewFile.ContentStream = fs;
        flciNewFile.Url = System.IO.Path.GetFileName(filePath);
        flciNewFile.Overwrite = true;

        List docs = web.Lists.GetByTitle(libraryName);
        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);

        ctx.Load(uploadFile);
        ctx.ExecuteQuery();
    }
}

还是不太行,成功后会再更新。当前错误是:

Could not find file 'F:approot12-09-2017.zip'.

最后

我正在使用来自 Amazon S3 的文件,所以解决方案是获取我的字节数据并将其流式传输到调用:

public void UploadDocumentContentStream(ClientContext ctx, string libraryName, string filename, byte[] data)
{

    Web web = ctx.Web;
    FileCreationInformation flciNewFile = new FileCreationInformation();
    flciNewFile.ContentStream = new MemoryStream(data); ;
    flciNewFile.Url = filename;
    flciNewFile.Overwrite = true;
    List docs = web.Lists.GetByTitle(libraryName);
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);
    ctx.Load(uploadFile);
    ctx.ExecuteQuery();

}

您可以使用 FileCreationInformation 创建一个新文件并通过 FileStream 提供内容。然后您可以将文件添加到目标库。这应该可以帮助您在使用上传方法时遇到大约 2mb 的限制。示例如下:

FileCreationInformation newFile = new FileCreationInformation
{
    Url = fileName,
    Overwrite = false,
    ContentStream = new FileStream(fileSourcePath, FileMode.Open)
};

var createdFile = list.RootFolder.Files.Add(newFile);
ctx.Load(createdFile);
ctx.ExecuteQuery();

在示例中,目标库是 list 您需要先获取对此的引用。如果需要,我可以向您展示如何执行此操作。