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

时间:2017-08-02 22:11:42

标签: c# .net sharepoint large-files

尝试通过SharePointClient upload远程在线上传文件到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();

}

1 个答案:

答案 0 :(得分: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,您需要首先获得对此的引用。如果需要,我可以告诉你如何做到这一点。