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 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误