Cakephp FormData Ajax file upload -
i'm stuck in uploading data , file using formdata + ajax + cakephp
here form:
<form name="multiform" class="frmaddproduct" action="addproduct" method="post" enctype="multipart/form-data"> <div class="formrow"> <div class="createproductlabel leftfloat">name</div> <div class="formcontrol leftfloat"><input type="text" name="name"/><br/><span>name?</span></div> <div class="clear"></div> </div> <div class="formrow"> <div class="createproductlabel leftfloat">cost</div> <div class="formcontrol leftfloat"><input type="text" name="cost"/></div> <div class="clear"></div> </div> <div class="formrow"> <div class="createproductlabel leftfloat">discount</div> <div class="formcontrol leftfloat"><input type="text" name="discount" class="discount" maxlength="5"/> %</div> <div class="clear"></div> </div> <div class="formrow"> <div class="createproductlabel leftfloat">description</div> <div class="formcontrol leftfloat"><textarea name="description" rows="5"></textarea></div> <div class="clear"></div> </div> <div class='formrow'> <div class='createproductlabel leftfloat'>choose file</div> <div class='formcontrol leftfloat'><input type='file' name=''productpicture'/></div> <div class='closephotos leftfloat'><img src='../img/close.png' class='closeproductpicture'/></div> <div class='clear'></div> </div> <div class="formrow"> <div class="createproductlabel leftfloat"> </div> <div class="formcontrol leftfloat"><input type="button" name="btncreateproduct" value=" create product "/></div> <div class="clear"></div> </div> </form>
here code submitting form:
$(".frmaddproduct").submit(function(e) { var formobj = $(this); var formurl = formobj.attr("action"); if(window.formdata !== undefined) // html5 browsers { var formdata = new formdata(this); $.ajax({ url: formurl, type: 'post', data: formdata, mimetype:"multipart/form-data", contenttype: false, cache: false, processdata:false, success: function(data, textstatus, jqxhr) { $("#multi-msg").html('<pre><code>'+data+'</code></pre>'); }, error: function(jqxhr, textstatus, errorthrown) { $("#multi-msg").html('<pre><code class="prettyprint">ajax request failed<br/> textstatus='+textstatus+', errorthrown='+errorthrown+'</code></pre>'); } }); e.preventdefault(); e.unbind(); } else // older browsers { // generate random id var iframeid = 'unique' + (new date().gettime()); // create empty iframe var iframe = $('<iframe src="javascript:false;" name="'+iframeid+'" />'); // hide iframe.hide(); // set form target iframe formobj.attr('target',iframeid); // add iframe body iframe.appendto('body'); iframe.load(function(e) { var doc = getdoc(iframe[0]); var docroot = doc.body ? doc.body : doc.documentelement; var data = docroot.innerhtml; $("#multi-msg").html('<pre><code>'+data+'</code></pre>'); }); } });
in firebug, can see data being sent:
-----------------------------2463414040929 content-disposition: form-data; name="categoryproduct" 1 -----------------------------2463414040929 content-disposition: form-data; name="companyproduct" 1 -----------------------------2463414040929 content-disposition: form-data; name="name" aaa -----------------------------2463414040929 content-disposition: form-data; name="cost" 34
but when try read or dump request data in controller, get: array()
i.e. no data being sent
debug($this->request->data);
also, $this->log($this->request->data["discount"]) gives me blank value
please me out on this.
i'm confused i'm getting wrong
i didn't used cakephp form helpers create form
use regular $_files php server variable
Comments
Post a Comment