【问题标题】:Yii framework with dropzone js带有 dropzone js 的 Yii 框架
【发布时间】:2018-05-27 16:51:55
【问题描述】:

我有一个带有图片上传功能的添加产品页面。 这个 sn-p 在我的视图文件中。

<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>

在我的 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) {

                }
            });
        }
    });

在我的 ProductController 中,我有这个功能:-

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

但是点击提交后dropzone js没有错误。页面只是像往常一样被重定向,就好像它根本没有通过“actionUploadProductImage”一样。

有人可以帮忙吗?谢谢

【问题讨论】:

    标签: javascript php jquery yii


    【解决方案1】:

    图像文件是从 $_FILES 获取的,但您使用的是 $_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
             
        }
    }
    

    【讨论】:

    • 非常感谢。我犯了多么愚蠢的错误!顺便说一句,如果我在函数中返回 false,那不应该在 dropzone 返回值中作为错误返回吗?
    猜你喜欢
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多