We are submitting an asp.net mvc view form using the knockoutjs postJson util. All works fine but now we want to add a file upload option in the form.
As per Uploading file in asp.net mvc, we added the elements and the enctype=”multipart/form-data” attribute in the Form but we do not get any files in the Request.Files collection in the Controller
Is there any issue using ko.utils.postJson? Is there any better way to upload files along with my form data?
- All my elements are using KO binds so cannot remove knockout
- We used postJson so that it can simulate a form submission and the controller can redirect to another view
The
postJsonsrc is doing aJSON.stringifyon each input it creates and inserting it as an input element in to an internal form element:As such, file data from a file input cannot be loaded via javascript to be posted via the
postJsoncall as it currently exists so your best bet for that would be to just upload the files and post the JSON in two separate requests. It may also be possible to inject the file input into this form thepostJsonmethod creates (by overriding the method) but its kind of hacky and may not be forward compatible.