2

Is there any way to upload a file to WCF web API endpoint? If so, how can I access Network Stream to read data?

Any help is very much appreciated.

Thanks.

aumanets
  • 3,703
  • 8
  • 39
  • 59

2 Answers2

4

Assuming you are talking about on the client side:

One way to do it is to create a FileContent class that derives from HttpContent. In the overrides you will be given the NetworkStream when the HTTPClient is ready to receive the stream.

If you are asking about the server side, then you can simply get hold of the HTTPRequestMessage and access the httpRequestMessage.Content.ContentReadStream property.


Update

By default the WCF Transport is limited to sending messages at 65K. If you want to send larger you need to enable "Streaming Transfer Mode" and you need to increase the size of MaxReceivedMessageSize, which is there just as a guard to prevent someone killing your server by uploading a massive file.

So, you can do this using binding configuration or you can do it in code. Here is one way to do it in code:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]);  //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • I don't think that WCF Web API Preview 4 has support to accept multipart/form-data as content type. I wasn't able to implement endpoint for file uploading. Hope this will be solved on next preview. – aumanets Jun 14 '11 at 19:42
  • You don't need to use multipart/form-data as the content type. You can just send the content as application/octet-stream – Darrel Miller Jun 14 '11 at 19:49
  • I've tried using application/octet-stream and without success. Now I'm not sure that this problem is caused by content-type. When I make requests, it doesn't reach configured end point and doesn't throws any exception. Maybe it's because data that is sent is larger than 65535 bytes? Do you know how to change this value on WCF Web API? – aumanets Jun 16 '11 at 09:49
  • Changing TransferMode and MaxReceivedMessageSize have solved this problem. Many thanks. – aumanets Jun 22 '11 at 19:42
  • @DarrelMiller I heard that Preview 5 has FileUpload feature build in. Anywhere that I can find an example implementation? – tugberk Sep 24 '11 at 04:37
  • @tugberk There are a bunch of examples here http://wcf.codeplex.com/SourceControl/changeset/view/1e77229b0064#WCFWebApi%2fHttp%2fTest%2fMicrosoft.Net.Http.Formatting%2fScenarios%2fMimeMultipart%2fProgram.cs – Darrel Miller Sep 24 '11 at 14:14
2

This answer is only designed to add some details with Web API preview 6.

TransferMode and MaxRecievedMessageSize are new exposed through the WebApiConfigurationClass.

var builder = new WebApiConfiguration();
builder.TransferMode = TransferMode.Streamed;
builder.MaxReceivedMessageSize = 1024 * 1024 * 10;

var serviceRoute = new WebApiRoute(route, new WebAPIServiceHostFactory() { Configuration = builder }, typeof(MyService));

I like it!

Eilistraee
  • 8,220
  • 1
  • 27
  • 30