简体   繁体   中英

Yii framework with dropzone js

I have an add product page with the image upload function. This snippet is in my view file.

<div class="well">
                <?php if ($modelProductitem->imagepath != '') { ?>
                <div class="row oc-img-exist">
                    <div class="col-xs-6 col-md-4 oc-thumbs col-xs-offset-4" data-product-id="<?php echo $modelProductitem->productid; ?>">
                        <a href="#4" class="oc-remove-icon"><i class="fa fa-close text-danger"></i></a>
                        <a href="#4" class="thumbnail">
                            <img src="<?php echo Octopus::getImagePathProductsWeb ().'/'.$productId.'/'.$modelProductitem->imagepath; ?>" title="<?php echo $modelProductitem->imagepath; ?>">
                        </a>
                    </div>
                </div>
                <?php } ?>
                <div class="col-md-12 drop-file dropzone oc-dropzone-previews dropzone-responsive">
                   x
                </div>
            <div class="clearfix"></div>

            </div>

in my JS

productDropzone = new Dropzone ('#frm-add-product', {

        url: '<?php echo Yii::app()->getBaseUrl (true); ?>/product/uploadProductImage',
        acceptedFiles: '.png, .PNG, .jpeg, .JPEG, .jpg, .JPG',
        autoProcessQueue: false,
        addRemoveLinks: true,
        uploadMultiple: true,
        previewsContainer: '.oc-dropzone-previews',
        clickable: '.drop-file',

        init: function() {
            this.on ('complete', function (file) {
                if (productDropzone.getUploadingFiles ().length === 0 && productDropzone.getQueuedFiles ().length === 0) {

                }
            });
        }
    });

In my ProductController , I have this function :-

public function actionUploadProductImage () {
        echo "test"; print_r($_POST); 
die();
    }

However after clicking on submit there's no error on the dropzone js. The page is just redirected as usual, as though it didn't go through the "actionUploadProductImage" at all.

Can anyone help? Thank you

The image files are getting from $_FILES but you are using $_POST

public function actionUploadProductImage () {
$ds          = DIRECTORY_SEPARATOR;  //1

$storeFolder = 'uploads';   //2

if (!empty($_FILES)) {

    $tempFile = $_FILES['file']['tmp_name'];          //3             

    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

    $targetFile =  $targetPath. $_FILES['file']['name'];  //5

    move_uploaded_file($tempFile,$targetFile); //6

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM