C#-CSOM-将文件上载到Sharepoint Online中的子目录
我正在尝试将文件上载到SharePoint Online,并获得以下示例代码:C#-CSOM-将文件上载到Sharepoint Online中的子目录,c#,sharepoint,upload,csom,C#,Sharepoint,Upload,Csom,我正在尝试将文件上载到SharePoint Online,并获得以下示例代码: var uploadFile = list .RootFolder .Folders .GetByPath(ResourcePath.FromDecodedUrl("A")) .Files .Add(newFile); ctx.Load(uploadFile); await c
var uploadFile = list
.RootFolder
.Folders
.GetByPath(ResourcePath.FromDecodedUrl("A"))
.Files
.Add(newFile);
ctx.Load(uploadFile);
await ctx.ExecuteQueryAsync();
它工作得非常好,所以它周围的一切都正常。但很明显,这个片段
var uploadFile = list
.RootFolder
.Folders
.GetByPath(ResourcePath.FromDecodedUrl("A/B"))
.Files
.Add(newFile);
ctx.Load(uploadFile);
await ctx.ExecuteQueryAsync();
即使两个目录都存在,也会引发System.IO.DirectoryNotFoundException。那么,如何将文件上载到子目录“B”?尝试像这样上载文件:
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);
}
请在此处查看另一个类似的线程:
我应该提到我们使用的是.NET core,而.SaveBinaryDirect()不可用。但是我们已经放弃了这个计划,因为它花费了太多的时间。@realemx,对于.NET核心解决方案,我已经在我的答案中更新了代码,请检查