Stephen Brannan - 423 - Jul 19, 2014

What’s the best way to upload a file to ServiceStack using angularjs (in my case I’m using the angular-file-upload module, which uses the html5 File API/FileReader). I was able to get it to upload to the service using the IRequiresRequestStream interface but it seems to include header information in the stream. Is there a better way to handle this? Or server side utils/methods to parse the content?

Here is a sample of the content I receive…

------WebKitFormBoundaryBNNXN1UqBUhgSAyT
Content-Disposition: form-data; name=“file”; filename="test.txt"
Content-Type: text/plain

this is a test.
-----WebKitFormBoundaryBNNXN1UqBUhgSAyT-

You can handle situations when what’s being POST’ed in the Request Body is not a DTO. E.g. you can post a raw file (e.g image) as a raw stream instead of being encoded in multi-part/form-data.

For multi-part form-data file uploads you can just use the Request.Files API, there’s an example on HttpBenchmarks: https://github.com/ServiceStack/HttpBenchmarks/blob/master/src/BenchmarksAnalyzer.ServiceInterface/AuthenticatedServices.cs#L251
And the ImageResizer example apps: https://github.com/ServiceStack/ServiceStack.UseCases/blob/master/ImageResizer/Global.asax.cs#L63

Stephen Brannan:

Ok I’ll give that a try. thanks.

Stephen Brannan:

Oh perfect! This worked great! Thanks so much! As always you provide top notch help and support!

Out of curiosity what situations is a IRequiresRequestStream useful?

It tells SS to skip normal Request DTO deserialization so you can do read from the body yourself, mentioned at: https://github.com/ServiceStack/ServiceStack/wiki/Serialization-deserialization#reading-directly-from-the-request-stream

Stephen Brannan:

Yeah I saw the docs on it. I was just wondering what use cases for it.