dongzongpeng6474 2014-10-20 09:48
浏览 33

在zf2中上传后显示图像缩略图

I am working on a project in zend framework.I Have created a registration form in which I need to upload user image,which I am able to do so,but my question is how to display that uploaded image through view?

Code: processAction()

$request = $this->getRequest();  
        if ($request->isPost()) {

            $project = new Projects();

            $nonFile = $request->getPost()->toArray();
            $File    = $this->params()->fromFiles('imageupload');
            $adapter = new \Zend\File\Transfer\Adapter\Http(); 

            //----------fetch name
            $project->exchangeArray($form->getData());
            $name=$project->name;

            //---------memcode generation
            $id=substr($project->name,0,1);
            $id=ucwords($id);
            $sm = $this->getServiceLocator();
            $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
            $sql="SELECT max(substr(memcode,2,5)) as maxcode from projects where substr(memcode,1,1)='$id'";
            $statement = $dbAdapter->query($sql);
            $result    = $statement->execute();
            $selectData = array();
            $abc = $result->current();
            foreach ($abc as $res) {
            $selectData[]=  $res;
            }
            if($selectData[0]=="")
            {
                $id.="00001";
            }
            else
            {
                $id.=str_pad(intval($selectData[0])+1,5,0,STR_PAD_LEFT);
            }

            $name=$id.time().'.jpeg';
            $data = array_merge(
                 $nonFile,
                 array('imageupload'=> $name)
             );

              $data = array_merge(
                $data,
                 array('memcode'=> $id)
             );
             var_dump($data);
            //break;
            //set data post and file ...    
            $form->setData($data);

            if ($form->isValid()) {

                $size = new Size(array('min'=>2000)); //minimum bytes filesize


                $adapter->setValidators(array($size), $File['name']);
                if (!$adapter->isValid()){
                    $dataError = $adapter->getMessages();
                    $error = array();
                    foreach($dataError as $key=>$row)
                    {
                        $error[] = $row;
                    }
                    $form->setMessages(array('imageupload'=>$error ));
                } 
                else 
                {
                    $adapter->setDestination('public/upload');         
                    $adapter->addFilter('File\Rename',
                        array('target' => $adapter->getDestination().'/'.$name,
                        'overwrite' => true));
                        if ($adapter->receive($File['name'])) {
                        $file = $adapter->getFilter('File\Rename')->getFile();
                        print_r($file[0]['target']);
                    }
                }  
            } 
          }

view code:

<?php
$this->headScript()->appendFile('/js/jquery.min.js');
echo $this->headScript();
?>
<script type="text/javascript">
jQuery(document).ready(function() {
$('#countrycode').val('1');


    $("#religioncode").change(function() {
        str=$(this).val();
        //alert(str);   
        $.ajax({
            type: "POST",
            dataType: 'json',
            url:"<?php echo $this->url(NULL,array('controller'=>'Register', 'action' =>'caste')); ?>",
            data:{
            religioncode:str},
            success: function(ajaxresult){
            var appenddata1="";
            $("#castecode").empty();
             ajaxresult=ajaxresult.caste;
             $("#castecode").append($('<option>Select Caste</option>'));
             $.each(ajaxresult, function (key, value) {
             $("#castecode").append($('<option></option>').val(value.castecode).text(value.castename));
        });          
            },
            error:function(){alert("Please select dropdown");}
            });

    });

        $("#email").change(function() {
        str=$(this).val();
                $.ajax({
            type: "POST",
            dataType: 'json',
            url:"<?php echo $this->url(NULL,array('controller'=>'Register', 'action' =>'emailajax')); ?>",
            data:{
            email:str
            },
            success: function(ajaxresult){
                ajaxresult=ajaxresult.memcode;

             $.each(ajaxresult, function (key, value) {
                    if(value!=""){
                        $("#email").val('');
                        alert("E-mail ID already exists");
                    }
                });     
            },  
        });
    });

    $("#countrycode").change(function() {
     str=$(this).val();
        //alert(str);   
        $.ajax({
            type: "POST",
            dataType: 'json',
            url:"<?php echo $this->url(NULL,array('controller'=>'Register', 'action' =>'city')); ?>",
            data:{
            countrycode:str},
            success: function(ajaxresult){
            //alert("success");
            if(str!=1)
            {
            $("#city").hide();
            }else{
            var appenddata1="";
            $("#citycode").empty();
             ajaxresult=ajaxresult.city;
             $("#citycode").append($('<option>Select City</option>'));
             $.each(ajaxresult, function (key, value) {
             $("#citycode").append($('<option></option>').val(value.citycode).text(value.cityname));
        });          
            }},
            error:function(){alert("Please select dropdown");}
            });
    });
});  

</script>
<html><body>
<section class="register">
<head><link rel="stylesheet" href="/css/demo.css" type="text/css" ></head>
<h2>Registration Form</h2>
<?php if ($this->error): ?>

<p class="error">
There were one or more issues with your submission.
Please correct them as
indicated below.
</p>
<?php endif ?>
<?php
$form = $this->form;
$form->prepare();
$form->setAttribute('action', $this->url(NULL,array('controller'=>'Register', 'action' =>'process')));
$form->setAttribute('method', 'post');
$form->setAttribute('enctype','multipart/form-data');
echo $this->form()->openTag($form);
?>
<dl class="zend_form">
<div align="left">
<div name="account"><b><u>Account Details</u></b>
<dt><?php echo $this->formLabel($form->get('email')); ?></dt>
<dd><?php
echo $this->formElement($form->get('email'));
echo $this->formElementErrors($form->get('email'));
?></dd>

<dt><?php echo $this->formLabel($form->get('password'));
?></dt>
<dd><?php
echo $this->formElement($form->get('password'));
echo $this->formElementErrors($form->get('password'));
?></dd>

<dt><?php echo $this->formLabel($form->get('confirm_password')); ?></dt>
<dd><?php
echo $this->formElement($form->get('confirm_password'));
echo $this->formElementErrors($form->get('confirm_password'));
?>

</dd>



</div>

<hr>
<div name="personal"><b><u>Personal Details</u></b>
<dt><?php echo $this->formLabel($form->get('name')); ?></dt>
<dd><?php
echo $this->formElement($form->get('name'));
echo $this->formElementErrors($form->get('name'));
?></dd>

<dt><?php echo $this->formLabel($form->get('gender')); ?></dt>
<dd><?php
echo $this->formElement($form->get('gender'));
echo $this->formElementErrors($form->get('gender'));
?></dd>

<dt><?php echo $this->formLabel($form->get('dob')); ?></dt>
<dd><?php
echo $this->formElement($form->get('dob'));
echo $this->formElementErrors($form->get('dob'));
?></dd>

<dt><?php echo $this->formLabel($form->get('mobile')); ?></dt>
<dd><?php
echo $this->formElement($form->get('mobile'));
echo $this->formElementErrors($form->get('mobile'));
?></dd>

<dt><?php echo $this->formLabel($form->get('heightcode')); ?></dt>
<dd><?php
echo $this->formElement($form->get('heightcode'));
echo $this->formElementErrors($form->get('heightcode'));
?></dd>


<dt><?php echo $this->formLabel($form->get('countrycode')); ?></dt>
<dd><?php
echo $this->formElement($form->get('countrycode'));
echo $this->formElementErrors($form->get('countrycode'));
?></dd>
<div id="city" name="city">
<dt><?php echo $this->formLabel($form->get('citycode')); ?></dt>
<dd><?php
echo $this->formElement($form->get('citycode'));
echo $this->formElementErrors($form->get('citycode'));
?></dd>
</div>

<dt><?php echo $this->formLabel($form->get('imageupload')); ?></dt>
<dd><?php
echo $this->formElement($form->get('imageupload'));
echo $this->formElementErrors($form->get('imageupload'));
?></dd>
</div>
<hr>

<div name="social"><b><u>Social details</u></b>
<dt><?php echo $this->formLabel($form->get('maritalstatus')); ?></dt>
<dd><?php
echo $this->formElement($form->get('maritalstatus'));
echo $this->formElementErrors($form->get('maritalstatus'));
?></dd>

<dt><?php echo $this->formLabel($form->get('mothertongue')); ?></dt>
<dd><?php
echo $this->formElement($form->get('mothertongue'));
echo $this->formElementErrors($form->get('mothertongue'));
?></dd>

<dt><?php echo $this->formLabel($form->get('religioncode')); ?></dt>
<dd><?php
echo $this->formElement($form->get('religioncode'));
echo $this->formElementErrors($form->get('religioncode'));
?></dd>

<dt><?php echo $this->formLabel($form->get('castecode')); ?></dt>
<dd><?php
echo $this->formElement($form->get('castecode'));
echo $this->formElementErrors($form->get('castecode'));
?></dd>

<dt><?php echo $this->formLabel($form->get('castenobar')); ?></dt>
<dd><?php
echo $this->formElement($form->get('castenobar'));
echo $this->formElementErrors($form->get('castenobar'));
?></dd>

</div>
<hr>
<dt><?php echo $this->formLabel($form->get('captcha')); ?></dt>
<dd><?php
echo $this->formElement($form->get('captcha'));

echo $this->formElementErrors($form->get('captcha'));
?></dd>

<dd><?php
echo $this->formElement($form->get('submit'));
echo $this->formElementErrors($form->get('submit'));
?></dd>
</dl>
</div>
<?php echo $this->form()->closeTag() ?>
</section>
  • 写回答

1条回答 默认 最新

  • dongtou8736 2014-10-21 06:50
    关注

    Using the code of view as shown below I am able to display the image after uploading

        <script type="text/javascript">
            function showimagepreview(input) {
            if (input.files && input.files[0]) {
            var filerdr = new FileReader();
            filerdr.onload = function(e) {
            $('#imageupload').attr('src', e.target.result);
            }
            filerdr.readAsDataURL(input.files[0]);
            }
            }
            </script>
    <input type="file" name="filUpload" id="filUpload" onchange="showimagepreview(this)" />
    <img id="imageupload" alt="uploaded image preview" height="100px" width="100px"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀