WebDavClient
WebDavClient copied to clipboard
PutFile creates an empty file
I'm sharing this to hopefully help others scratching their heads with a similar issue. I was changing code originally using HttpWebRequest, in order to tidy up & make use of your library instead.
In my example I have 2 MemoryStreams, stream1 has dummy text as used in your example code, and stream 2 contains some serialized xml.
var stream1 = new MemoryStream(Encoding.UTF8.GetBytes("<content/>"));
var stream2 = new MemoryStream();
var streamWriter = new StreamWriter(stream, System.Text.Encoding.UTF8)
serializer.Serialize(streamWriter, sampleXml);
var resPutFile1 = await client.PutFile(exportFile1, stream1);
var resPutFile2 = await client.PutFile(exportFile2, stream2);
Stream 1 was succesfully written to exportFile1. Stream 2 wasn't written to exportFile2 - the file itself was created but with no content.
The issue (fixed) was with the memory stream position - it needed to be set to 0 before calling PutFile, in order to read the whole stream and write it out to the file.
stream2.Position = 0;
var resPutFile2 = await client.PutFile(exportFile2, stream2);
I thought thought I'd flag this since HttpWebRequest happily seems to take care of that itself, and I never needed to specify the position, it just reads the stream from the beginning.
@codePenny thank you for sharing this.
This had me stuck for a few hours today, thanks for sharing