jquery - How can I submit a multipart form via Ajax into web2py? (form includes file(s)) -


i have process arbitrary file, not related model. so, let's want full control on form , data sends. have "manually built" form in controller:

the_form = form(input(_type='file', _name='image'),                 input(_type='text', _name='sometext'),                 _action=url('controller', 'upload_test'),                 _method='post') 

in view, display form normally, with, let's {{=the_form}} then, using simple jquery, try submit form via ajax.

however, i've encountered following problems:

  • the file doesn't seem through server;
  • eventually i've found there request._body.file appears contain uploaded file, "access denied" when trying read (it's temporary file); , doesn't feel right using it.

why doesn't file through server side in regular request.vars or request.post_vars ? appreciated. thank you.

i managed resolve myself. "trick", if may so, send data server in proper way, serializing form data differently. here's brief description of that:

(function($) { $.fn.serializefiles = function() {     var obj = $(this);     var formdata = new formdata();     var params = $(obj).serializearray();      $.each($(obj).find("input[type='file']"), function(i, tag) {         $.each($(tag)[0].files, function(i, file) {             formdata.append(tag.name, file);         });     });      $.each(params, function (i, val) {         formdata.append(val.name, val.value);     });     return formdata; }; })(jquery);  var form_request_data = $("form").serializefiles();  var ajaxparams = {         type: "post",         url : target_url,         data : form_request_data,         cache: false,         contenttype: false,         processdata: false,         async : true };  $.ajax(ajaxparams).done( function(request_result) {  ...  } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -