Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
knockoutjs: envíe MultipartFile y otros datos con una llamada ajax

tengo este sencillo formulario:

 <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>

y mi modelo de vista:

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

con knockout, la var picture contiene solo la ruta del archivo, con kendo puedo tomar el archivo con este código:

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

Quiero enviar todos mis datos a un controlador Java. He probado más controladores... este es el último tri:

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

El usuario es una clase simple con nombre de cadena, apellido;

Esta es mi llamada ajax:

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

¿Cuál es la forma correcta de enviar mis datos y configurar la llamada ajax? ¿Cómo puedo cargar el archivo a través del enlace de datos de knockout y no mediante kendoUpload?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No estoy seguro si entiendo totalmente, pero esto es lo que hago cuando necesito enviar más datos que solo la matriz MultipartFile.

 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 } }); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!