简体   繁体   English

将文件上传到UWP项目中的WebDAV

[英]Upload file to WebDAV in UWP project

I am trying to implement a write operation on WebDAV storage using Portable-WebDAV-Library but my code 我试图使用Portable-WebDAV-Library在我的代码上实现对WebDAV存储的写操作

var credentials = new NetworkCredential("user", "password");
var webDavSession = new WebDavSession(@"https://...", credentials);
Uri uri = new Uri("https://.../SubDir/somefile.txt");

try
{
  using (MemoryStream ms = new MemoryStream())
  {
    using (StreamWriter sw = new StreamWriter(ms))
    {
      sw.Write(DateTime.Now.ToString("G"));
      sw.Flush();
      ms.Position = 0;
    }

    await webDavSession.UploadFileAsync(uri, ms);
  }
}
catch (Exception ex)
{
}

throws the exception "Cannot use the specified Stream as a Windows Runtime IInputStream because this Stream is not readable." 抛出异常“无法将指定的Stream用作Windows运行时IInputStream,因为此Stream不可读。” I have experimented with IRandomAccessStream and IOutputStream but that yielded other exceptions. 我已经尝试过IRandomAccessStream和IOutputStream但是产生了其他异常。

The problem was, that the StreamWriter closed the MemoryStream at the end of its using block. 问题是, StreamWriter在其使用块结束时关闭了MemoryStream So moving the } from before to behind of await webDavSession.UploadFileAsync(uri, ms); 因此,从await webDavSession.UploadFileAsync(uri, ms);之前移动} await webDavSession.UploadFileAsync(uri, ms); solved the problem: 解决了这个问题:

Before: 之前:

  ms.Position = 0;
}

await webDavSession.UploadFileAsync(uri, ms);

After: 后:

  ms.Position = 0;
  await webDavSession.UploadFileAsync(uri, ms);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM