1

i have this simple form:

<form id="form">
<ul>
    <li><label>Picture</label>
        <input id="upload" name="Picture" class="k-textbox" data-bind="value: picture" type="file"/>
    </li>
    <li>
        <label>Name</label>
        <input type="text" data-bind="value: name" id="name" class="k-textbox" />
    </li>
    <li>
        <label>Surname</label>
        <input type="text" data-bind="value: surname" id="surname" class="k-textbox" />
    </li>
</ul>
</form>

and my viewModel:

var ViewModel = funtion(){
    this.picture = ko.observableArray();
    this.name = ko.observable();
    this.surname = ko.observable():
};

with knockout the var picture contains only the path of the file, with kendo i can take the file with this code:

var kendoUpload = $("#upload").kendoUpload({
    multiple: false,
});
var files = kendoUpload.data("kendoUpload").getFiles()[0];

i want send all my data to a java controller. I have try more controller...this is the last tri:

@RequestMapping(value = "addUser")
public String addUser(MultipartHttpServletRequest request, @RequestBody User user, @RequestParam("picture") MultipartFile[] uploadFiles)
        throws Exception {

    return "finish";
}

User is a simple class with String name, surname;

This is my ajax call:

$.ajax({
url : "./user/addUser",
type : "POST",
dataType :'multipart/form-data',
contentType : "application/json",
useProxy: false,
data : data
 }).done(function(response) {
    console.log(response);
 });

What is the correct way to send my data and set the ajax call? How do I get the file uploaded through the data-bind of knockout and not by kendoUpload?

Odino
  • 141
  • 1
  • 6
  • 17

1 Answers1

1

Not sure if I totally understand, but this is what I do when I need to send more data than just the MultipartFile array.

function submitUser(user) {

    // input where the files were uploaded to
    var filesToUpload = filesInput[0].files;

    // new form with our files and our user object
    var formData = new FormData();
    formData.append("productMedia", filesToUpload);
    formData.append("user", user);

    // ajax request
    var request = $.ajax({
        url: './user/addUser',
        data: formData,
        // Tell jQuery not to process data or not to worry about content-type
        // You *must* include these options in order to send MultipartFile objects
        cache: false,
        contentType: false,
        processData: false,
        method: 'POST',
        type: 'POST',
        success: function() {
            // do something
        },
        error: function () {
            // do something
        }
    });
}
Alain Cruz
  • 4,757
  • 3
  • 25
  • 43