简体   繁体   中英

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

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." I have experimented with IRandomAccessStream and IOutputStream but that yielded other exceptions.

The problem was, that the StreamWriter closed the MemoryStream at the end of its using block. So moving the } from before to behind of 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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