WebClient.UploadFile download file as stream

I am trying to use WebClient.UploadFile in my project to send a file to the server. WebClient.UploadFile accepts the uri file name as a parameter, but I would like to pass the file stream instead of the uri file name. Is this possible with WebClient?

+6
source share
3 answers

You must use the WebClient.OpenWrite and OpenWriteAsync methods to send the stream back to your server.

If you are using later versions, subscribe to OpenWriteCompleted and use e.Result as a stream to CopyTo .

+1
source

Here are a few examples that show how to write a stream to a specified resource using the WebClient class :

Using WebClient.OpenWrite :

 using (var client = new WebClient()) { var fileContent = System.IO.File.ReadAllBytes(fileName); using (var postStream = client.OpenWrite(endpointUrl)) { postStream.Write(fileContent, 0, fileContent.Length); } } 

Using WebClient.OpenWriteAsync :

 using (var client = new WebClient()) { client.OpenWriteCompleted += (sender, e) => { var fileContent = System.IO.File.ReadAllBytes(fileName); using (var postStream = e.Result) { postStream.Write(fileContent, 0, fileContent.Length); } }; client.OpenWriteAsync(new Uri(endpointUrl)); } 
+2
source

This is not possible, but what you offer is a good idea. I would go to http://connect.microsoft.com and register a feature request.

0
source

Source: https://habr.com/ru/post/906084/


All Articles