【问题标题】:C# - CSOM - Upload a file to a subdirectory in Sharepoint OnlineC# - CSOM - 将文件上传到 Sharepoint Online 中的子目录
【发布时间】:2021-01-22 06:24:30
【问题描述】:

我正在尝试将文件上传到 SharePoint Online,我得到了这个示例代码:

var uploadFile = list
            .RootFolder
            .Folders
            .GetByPath(ResourcePath.FromDecodedUrl("A"))
            .Files
            .Add(newFile);

ctx.Load(uploadFile);
await ctx.ExecuteQueryAsync();

它工作得非常好,所以它周围的一切都可以工作。但显然这个sn-p

var uploadFile = list
            .RootFolder
            .Folders
            .GetByPath(ResourcePath.FromDecodedUrl("A/B"))
            .Files
            .Add(newFile);

ctx.Load(uploadFile);
await ctx.ExecuteQueryAsync();

即使两个目录都存在,也会引发 System.IO.DirectoryNotFoundException。那么如何将文件上传到子目录“B”?

【问题讨论】:

    标签: c# sharepoint upload csom


    【解决方案1】:

    尝试像这样上传文件:

     public static void UploadFile(ClientContext context,string uploadFolderUrl, string uploadFilePath)
    {
        var fileCreationInfo = new FileCreationInformation
        {
                Content = System.IO.File.ReadAllBytes(uploadFilePath),
                Overwrite = true,
                Url = Path.GetFileName(uploadFilePath)
        };
        var targetFolder = context.Web.GetFolderByServerRelativeUrl(uploadFolderUrl);
        var uploadFile = targetFolder.Files.Add(fileCreationInfo);
        context.Load(uploadFile);
        context.ExecuteQuery();
    }
    
    using (var ctx = new ClientContext(webUri))
    {
         ctx.Credentials = credentials;
    
         UploadFile(ctx,"LibName/FolderName/Sub Folder Name/Sub Sub Folder Name/Sub Sub Sub Folder Name",filePath);   
    }
    

    请在此处查看另一个类似的帖子:

    Alternative Save/OpenBinaryDirect methods for CSOM for SharePoint Online

    【讨论】:

    • 我应该提到我们正在使用 .NET 核心并且 .SaveBinaryDirect() 不可用。但我们已经取消了该计划,因为它花费了太多时间。
    • @realemx,对于 .NET Core 解决方案,我已经更新了答案中的代码,请检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2023-03-19
    • 2018-12-28
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多