簡體   English   中英

Yii2:如何從兩個字段中上傳兩個圖像的形式

[英]Yii2: how to upload two images from two fields in form

從控制器創建功能

$imageName = $model->primary_name;
// get the uploaded file instance. for multiple file uploads  
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);          
//save the path in db
$model->id_image = 'uploads/'.$imageName.'.'.$model->file->extension;

$imageName1 = $model->adult_2_name;
// get the uploaded file instance. for multiple file uploads
$model->file2 = UploadedFile::getInstance($model, 'file2');
$model->file2->saveAs('uploads/adult_2/'.$imageName1.'.'.$model->file2->extension);
//save the path in db
$model->id_image_2 = 'uploads/adult_2/'.$imageName1.'.'.$model->file2->extension;

$model->save();

模型

public $file;
public $file2;

[['file','file2'],'file'],
[['id_image', 'id_image_2'], 'string', 'max' => 500],

視圖

     <?php

            use yii\helpers\Html;
            /*use yii\widgets\ActiveForm;*/
            use kartik\form\ActiveForm;
            use kartik\file\FileInput;
            use yii\helpers\Url;
            use yii\bootstrap\Modal;

            /* @var $this yii\web\View */
            /* @var $model backend\models\CreateBookings */
            /* @var $form yii\widgets\ActiveForm */
            ?>

 <div class="create-bookings-form">

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

<?= Html::activeHiddenInput($model, 'booking_id', ['value' => '' ]) ?>
    <?= $form->field($model, 'check_in')->label(false)->input('date', ['placeholder' => '']) ?>

    <?= $form->field($model, 'check_out')->label(false)->input('date', ['placeholder' => '']) ?>

    <?= $form->field($model, 'room_type')->label(false)->input('text', ['placeholder' => 'Select Room Type...']) ?>
    <?= $form->field($model, 'primary_name')->label(false)->textInput(['maxlength' => true]) ?>
    <?php $list = ['Male' => 'Male', 'Female' => 'Female'];
    echo $form->field($model, 'gender_1')->label(false)->radioList($list, ['inline'=>true]); ?>
    <?php   echo $form->field($model, 'primary_mobile', ['feedbackIcon' => ['default' => 'phone']])->label(false)->textInput(['placeholder'=>'Enter phone number...']); ?>
    <?php   echo $form->field($model, 'primary_email', [
                            'feedbackIcon' => [
                            'default' => 'envelope',
                            'success' => 'ok',
                            'error' => 'exclamation-sign',
                            'defaultOptions' => ['class'=>'text-primary']
                                ]
                                ])->label(false)->textInput(['placeholder'=>'Enter a valid email address...']); ?>

    <?php echo $form->field($model, 'id_type')->label(false)->dropDownList(['Passport' => 'Passport', 'Aadhar Card' => 'Aadhar Card', 'Driving Licence' => 'Driving Licence','Voter ID' => 'Voter ID',]); ?>
    <?= $form->field($model, 'id_number')->label(false)->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'file')->label(false)->fileInput(['id'=>'imgfile','onchange' => 'readURL(this)']); ?>                  
    <?php echo '<img id="preview" src="#" width="100" height="100" alt="your image" />' ; ?>                 
    <?= $form->field($model, 'adult_2_name')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'file')->fileInput(
                    [
                    'id'=>'imgfile2',
                    'onchange' => 'readURL(this)'
                    ]); 
                ?>
    <?= '<img id="preview2" src="#" width="100" height="100" alt="your image" />' ?>

    <div class="form-group">
                    <?= Html::submitButton($model->isNewRecord ?  'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                </div>

                <?php ActiveForm::end(); ?>

            </div>

            <?php
            $script = <<< JS

            $("#imgfile").change(function(){
                if (this.files && this.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $('#preview').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });

            JS;
            $this->registerJs($script);
            ?>
            <?php
            $script = <<< JS

            $("#imgfile2").change(function(){
                if (this.files && this.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $('#preview2').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });

            JS;
            $this->registerJs($script);
            ?>

嘗試在數據庫中上傳兩個圖像,但無法成功。

出現錯誤:

在字符串上調用成員函數saveAs()

更新了活動表格....................

改變這一行:

$model->file = UploadedFile::getInstance($model, 'file2');

$model->file$model->file2


更新

並且在您的視圖文件上,第二個輸入:

<?= $form->field($model, 'file')->fileInput(
    [ 
      'id'=>'imgfile2',
      'onchange' => 'readURL(this)'
    ]); 
?>

應該與file2相關,否則您將覆蓋第一個上傳的文件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM