javascript - How to upload multiple files to server using AJAX and php -
i used magento mvc framework , trying upload canvas todataurl image using ajax. here controller,
public function uploadimgaction() { $mediaurl = mage::getbaseurl(mage_core_model_store::url_type_media); $message = ''; $filenames = array(); $imagename = ''; $successcount = 0; $time = date('m_d_y_hia'); if(isset($_post['email']) && !empty($_post['email'])) { $imagename = str_replace(array('@','.'), '_', $_post['email']).'_'; } else { $imagename = 'unknown_user_'; } if (isset($_files) && !empty($_files)) { foreach ($_files $key => $value) { try { $uploader = new varien_file_uploader($key); $uploader->setallowedextensions(array('png', 'jpg','gif')); $uploader->setallowrenamefiles(true); $uploader->setfilesdispersion(false); // set upload folder name current date $currenttimestamp = mage::getmodel('core/date')->timestamp(time()); $folder_name_date = date('y_m_d', $currenttimestamp); // create folder if not exsists $path = mage::getbasedir('media') . ds . 'uploads' . ds . $folder_name_date; if(!is_dir($path)){ mkdir($path, 0777, true); } // image name $ext = pathinfo($_files[$key]['name'], pathinfo_extension); $tmp = $imagename.'_'.$successcount.'_'.$time.'.'.$ext; // save file $uploader->save($path, $tmp); $newfilename = $uploader->getuploadedfilename(); // append return $url = $mediaurl.'uploads'.'/'.$folder_name_date.'/'.$tmp; $filenames[] = $url; $successcount++; } catch (exception $e) { $message .= 'file upload error '; } } // number of files uploaded correctly $message = $successcount . ' files uploaded successfully'; } else { $message .= 'file not set '; } // return json if($successcount>1) { echo $this->_uploadsjsonresponse($message,$filenames,'true'); } else { echo $this->_uploadsjsonresponse($message,$filenames,'false'); } }
i not have ajax knowledge not work properly. here ajax. think data passing methods not set in ajax. need know how append formdata in array.
var printimg = canvas.todataurl({ format: 'png', multiplier: multi, left: (canvas.width - maskwidth)/2, height: maskoriheight/multi, width: maskoriwidth/multi }); var formdata = new formdata(); formdata.append('imagename[]', printimg[0]); formdata.append('email', 'ex@gmail.com'); $.ajax({ type: "post", //url: "upload.php", url: "http://example.com/api/index/uploadimg", //data: {image: printimg, email: email} data: formdata, processdata: false, contenttype: false }) .done(function(respond) { console.log('saved'+respond); });
how this?
you should this:
$.ajax({ type: "post", url: "/yoururl", <---// "namecontroller/nameaction" data: data, datatype: 'html', success: function (result) { alert("your message sent!" + result); } });
Comments
Post a Comment