如何使用WebDav访问C#中的nextcloud文件?

如何使用WebDav访问C#中的nextcloud文件?,c#,webdav,winscp,nextcloud,C#,Webdav,Winscp,Nextcloud,当我试图通过WinSCP访问nextcloud的WebDav API时,在正确使用根文件夹、远程路径等方面遇到了几个问题。 为了节省其他人的时间,下面是我提出的将文件上传到远程(共享)文件夹的工作代码 经验教训: 提供的服务器名称没有协议,这是由 会话选项。协议 根文件夹不能为空,必须至少为“/” 下一个云提供程序/配置定义根url,因此在remote.php之后预定义“webdav”或“dav”。通常,在设置部分使用nextcloud的webapp时,您可以在左下角看到它 “文件/用户”或“文

当我试图通过WinSCP访问nextcloud的WebDav API时,在正确使用根文件夹、远程路径等方面遇到了几个问题。 为了节省其他人的时间,下面是我提出的将文件上传到远程(共享)文件夹的工作代码

经验教训:

  • 提供的服务器名称没有协议,这是由 会话选项。协议
  • 根文件夹不能为空,必须至少为“/”
  • 下一个云提供程序/配置定义根url,因此在remote.php之后预定义“webdav”或“dav”。通常,在设置部分使用nextcloud的webapp时,您可以在左下角看到它
  • “文件/用户”或“文件/用户名”不一定是必需的-也由主机/配置定义
  • 连接用户必须具有对给定目录的访问权限。您应该通过在TransferOptions中提供文件权限向其他人提供文件访问权限(如果需要)
  • 但是,在WinSCP、nextcloud文档中没有工作示例,在这里也找不到任何工作示例。

    //设置会话选项
    
    // Setup session options
    var sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Webdav,
        HostName = server,
        WebdavRoot = "/remote.php/webdav/" 
        UserName = user,
        Password = pass,
    };
    
    using (var session = new Session())
    {
        // Connect
        session.Open(sessionOptions);
    
        var files = Directory.GetFiles(sourceFolder);
    
        logger.DebugFormat("Got {0} files for uploading to nextcloud from folder <{1}>", files.Length, sourceFolder);
    
        TransferOptions tOptions = new TransferOptions();
        tOptions.TransferMode = TransferMode.Binary;
        tOptions.FilePermissions = new FilePermissions() { OtherRead = true, GroupRead = true, UserRead = true };
    
        string fileName = string.Empty;
        TransferOperationResult result = null;
    
        foreach (var localFile in files)
        {
            try
            {
                fileName = Path.GetFileName(localFile);
    
                result = session.PutFiles(localFile, string.Format("{0}/{1}", remotePath, fileName), false, tOptions);
    
                if (result.IsSuccess)
                {
                    result.Check();
    
                    logger.DebugFormat("Uploaded file <{0}> to {1}", Path.GetFileName(localFile), result.Transfers[0].Destination);
                }
                else
                {
                    logger.DebugFormat("Error uploadin file <{0}>: {1}", fileName, result.Failures?.FirstOrDefault().Message);
                }
            }
            catch (Exception ex)
            {
                logger.DebugFormat("Error uploading file <{0}>: {1}", Path.GetFileName(localFile), ex.Message);
            }
        }
    }
    
    var sessionOptions=新sessionOptions { 协议=协议.Webdav, 主机名=服务器, WebdavRoot=“/remote.php/webdav/” 用户名=用户, 密码=通过, }; 使用(var session=newsession()) { //连接 会议。公开(会议选项); var files=Directory.GetFiles(sourceFolder); DebugFormat(“获取{0}个文件,用于从文件夹上载到nextcloud”,files.Length,sourceFolder); TransferOptions TopOptions=新的TransferOptions(); tOptions.TransferMode=TransferMode.Binary; tOptions.FilePermissions=newfilepermissions(){OtherRead=true,GroupRead=true,UserRead=true}; 字符串文件名=string.Empty; TransferOperationResult=null; foreach(文件中的var localFile) { 尝试 { fileName=Path.GetFileName(localFile); 结果=session.PutFiles(localFile,string.Format(“{0}/{1}”,remotePath,fileName),false,tOptions); 如果(结果。发布成功) { result.Check(); DebugFormat(“将文件上载到{1}”,Path.GetFileName(localFile),result.Transfers[0]。Destination); } 其他的 { DebugFormat(“错误上载到文件:{1}”,文件名,result.Failures?.FirstOrDefault().Message); } } 捕获(例外情况除外) { DebugFormat(“上传文件时出错:{1}”,Path.GetFileName(localFile),ex.Message); } } }

    希望这能为其他人节省一些时间。

    请将帖子编辑成一个问题和一个单独的答案,例如“如何访问…”再加一点肉。然后将解决方案作为答案发布。然后它将适合网站的格式。也要看一看,并采取编辑完成-不改变上下文。。。。