We’re trying to upload a file and post some JSON data in a DTO to a ServiceStack endpoint from Angular JS. The endpoint does work and we can upload files to it using C#, but for some reason this does not seem to be as easy from Angular JS.
Are there any tricks we should know about? On the .NET side, the SS endpoint simply reads from the Request.Files collection. In JS, should we be:
Using FileReader to readAsBinaryString and then send that to the endpoint.
Using FileReader to readAsArrayBuffer then convert the byte array into a binary string and then send that to the endpoint.
Using FileReader to readAsDataURL into Base64 and then convert the Base64 into a binary string and then send that to the endpoint.
ServiceStack just processes standard HTTP File Uploads, i.e. as sent using HTML’s native multipart/form-data FORMs which will then be accessible in ASP.NET’s Request.Files collection so you should look at using a solution that simulates that, e.g. You can use Fine Uploader component to easily upload multiple files.
We’re not even trying to upload multiple files, just the one file and the DTO payload in the POST. I’ll pass this message onto the frontend team who are currently trying to figure this out. When they’re back online I’ll get them to pitch in here if this doesn’t help.
For what it’s worth, my team has been using FineUploader from within our AngularJS app to upload files to our ServiceStack back end for many years now.
We never bothered doing anything in plain JavaScript (although I’m sure it’s possible). We just wrote a simple custom directive that wrapped FineUploader and away we went.
There are a number of odd edge cases and browser quirks that FineUploader handles for you, so I quite recommend it.
@codefrenzy did you ever get this working with the inbuilt servicestack js clients (i.e. using the jsclient.post<dto>(someRequestMsgObj) approach, with the files attached somehow) , or did you have to write a custom upload function in a more traditional ajax style?
Never mind, i used Typescript Servicestack-client post file and request and got it working.
Note to anyone trying this approach however: in contrast to many of the older AJAX multipart examples you will find, DO NOT put ‘Content-Type’: ‘multipart/form-data’ as a header property or it wont work! I spent so much time on this