doutang1873 2016-05-04 16:51
浏览 149
已采纳

使用yii2和kartik fileInput扩展名上传多个图像

I have this little problem in uploading multiple images using yii2 and Kartik fileInput extension.

this is the model:

 public $file;

public static function tableName()
{
    return 'news';
}

public function rules()
{
    return [
        [['news_desc', 'news_context', 'news_first_source', 'news_second_source', 'news_third_source', 'news_responsibe_party', 'news_first_testimony', 'news_second_testimony', 'news_body', 'news_lang'], 'string'],
        [['news_gov_id', 'news_typ_id', 'news_is_in_slider', 'news_is_act', 'news_is_del'], 'integer'],
        [['news_happened_date', 'news_created_date', 'file'], 'safe'],
        [['news_typ_id', 'news_lang'], 'required'],
        [['news_title'], 'string', 'max' => 255],
        [['file'], 'file','maxFiles' => 6],
    ];
}

as you can notice I am using maxFiles but it didnt worked for me

Controller:

public function actionCreate_news()
{

    $model = new News();

    if ($model->load(Yii::$app->request->post())) {
        $model->file = UploadedFile::getInstances($model, 'file');

        var_dump($model->file);
        die();}}

right now I am just var dump the files I get but the problem there is only one file not all the files i uploaded

view:

echo FileInput::widget([
            'model' => $model,
            'attribute' => 'file[]',
            'name' => 'file[]',
            'options' => [
                'multiple' => 'true',
                'accept' => 'image/*'
            ],
            'pluginOptions' => [
                'showCaption' => false,
                'showRemove' => false,
                'showUpload' => false,
                'allowedFileExtensions' => ['jpg','jpeg','png'],
                'overwriteInitial' => false
            ],
        ]);

i have read all things about this issue and tried all the possible solutions but the problem is still there

when i press submit for the form only the last file will be submited

thanks

  • 写回答

2条回答 默认 最新

  • dsdvr06648 2016-05-05 14:59
    关注

    Thank you all I figured out the solution for my question i was trying to upload the images when submitting the form but it turned that i was wrong so i used the widget to upload the images using ajax like in here: http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/

    then using the plugin event i took the names of files to save in the DB when the form submited

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题