【问题标题】:SharePoint Online, uploading files larger than 2mb using SharePointClientSharePoint Online,使用 SharePointClient 上传大于 2mb 的文件
【发布时间】:2018-01-10 07:54:28
【问题描述】:

在尝试通过SharePointClientupload 远程在线将文件上传到 SharePoint 时,我遇到了 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();

}

【问题讨论】:

    标签: c# .net sharepoint large-files


    【解决方案1】:

    您可以使用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,您需要先获取对此的引用。如果需要,我可以向您展示如何执行此操作。

    【讨论】:

    • 谢谢!明天我会试一试,然后告诉你进展如何。
    • 要记住一件事,使用此方法会在 30 分钟后发生超时。 Microsoft 的指导是对最大 10mb 的文件使用文件流上传,但您可以使用更多。如果您仍然遇到问题,请告诉我,我将提供 Start、Continue、Finish 上传方法的示例。
    • 您好,我尝试了您所建议的稍作修改的版本。请参阅上面的更新。但是,我从 SharePoint 中看到以下错误:SharePoint 错误:找不到文件“F:approot12-09-2017.zip”。我上传的文件是“12-09-2017.zip”。 “F:approot”是从哪里来的?
    • filePath 变量包含什么?代码是否异常?如果是这样,您在哪一行看到错误?
    • 我认为问题在于我正在引用 S3 文件,因此我需要将字节数组流式传输到调用中,而不是使用文件选项。
    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 2021-12-06
    • 2019-01-20
    • 2017-06-01
    • 2013-12-14
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多