第一步: 搭建上传类基础工作,具体请看://www.jb51.net/article/120242.htm

第二步:建站一个product表,字段id,name,picurl.

第三步:生GII生成PRODUCT 模型,类,视图。

第四步:

main.css 放在frontend\web\css

.onedialog{position:absolute; left: 300px; top: 500px; z-index: 10; width: 700px; height: 400px;border-radius:5px;box-shadow:5px 2px 6px #000; border: 2px solid #666}
.oneiframe{ width: 100%; height: 100% }

main.js 放在 frontend\web\assets

$(function(){
 $('#product-picurl').click(function(){
 $('#oneupload').remove();
 $('<div>').appendTo($('body')).attr({"class":"onedialog",'id':"oneupload"});
 $('<iframe>').appendTo($('#oneupload')).attr({"src":"","class":"oneiframe"})
 });
  var v=$('#product-picurl').val();
 if(v){
 $('<img>').attr({"src":v,"style":"height:50px"}).insertAfter($('#product-picurl'));
 }
});

然后在frontend\assets\AppAsset.php中注册这两个文件

class AppAsset extends AssetBundle
{
  public $basePath = '@webroot';
  public $baseUrl = '@web';
  public $css = [
    'css/site.css',
    'css/main.css',
  ];
  public $js = [
    'assets/main.js'
  ];
  public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapAsset',
  ];
}

UploadController.php

<"YmdHis") . '.' . $model->file->extension;
        $model->file->saveAs($fileName);
      }
      echo "<script src='assets/upload.js'></script>;";
      echo "<script>";
      echo "var oneinput=parent.document.getElementById('product-picurl');";
      echo "parent.document.getElementById('product-picurl').value='".$fileName."';";
      echo "var oneupload = parent.document.getElementById('oneupload');";
      echo "var img = document.createElement('img');";
      echo "img.setAttribute('style', 'height:50px');";
      echo "img.src ='".$fileName."';";
      echo "insertAfter(img,oneinput);";
      echo "oneupload.parentNode.removeChild(oneupload)";
      echo "</script>";
    }

    return $this->render('upload', ['model' => $model]);
  }
}
"htmlcode">
<"htmlcode">
<?php
use yii\widgets\ActiveForm;
?>

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

<?= $form->field($model, 'file')->fileInput() ?>

<button>Submit</button>

<?php ActiveForm::end() ?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
yii2,上传图片,yii2,上传

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“yii2 上传图片的示例代码”

暂无“yii2 上传图片的示例代码”评论...