我想在magento前端上传图片并通过ajax显示。但是我收到此错误TypeError: jQuery(...).ajaxForm is not a function
,这会在定义文件夹中上传图片并在前端显示时产生问题。
这是我的代码:
<script type="text/javascript" src="<?php echo Mage::getBaseUrl();?>/skin/frontend/rwd/default/js/jquery.form.js"></script>
<form id="imageform" method="post" enctype="multipart/form-data" action='<?php echo $this->getUrl('productcustomize/index/uploadimage/'); ?>'>
<div class="right">
<h3>UPLOAD YOUR PICTURES</h3>
<h4>Side Profile</h4>
<span>Sample Picture</span>
<input type="file" name="photoimg" id="photoimg" />
</div>
<div id='preview'></div>
</form>
<script type="text/javascript" >
jQuery(document).ready(function() {
jQuery('#photoimg').on('change', function() {
jQuery("#preview").html('');
jQuery("#preview").html('<img src="<?php echo Mage::getBaseUrl();?>/skin/frontend/base/default/lotusbreath/onestepcheckout/images/loader.gif" alt="Uploading...."/>');
jQuery("#imageform").ajaxForm({
data: jQuery(this).serialize(),
target: '#preview'
}).submit();
});
});
</script>
IndexController.php
public function uploadimageAction()
{
$path = getcwd()."/media/sideprofile/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = md5(date('Y-m-d H:i:s')).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$_SESSION['custom_image'][] = $actual_image_name;
$custom_image_data = $_SESSION['custom_image'];
foreach($custom_image_data as $key=>$img){
if($img !=""){
$resizeimg = Mage::getBaseUrl().'/media/sideprofile/uploads/'.$img;
}
}
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
}