yii xupload for CActiveForm的mysql

时间:2012-12-15 18:33:35

标签: yii upload blueimp yii-xupload

我试图在CActiveForm中包含yii的xupload扩展我试着遵循这个http://www.yiiframework.com/wiki/395/additional-form-data-with-xupload/ 但是我做不到。我有一个包含用户名和文件名字段的表单(允许用户从视图上传和查看它。) 这是我的看法

<?php
$form = $this->beginWidget('CActiveForm', array(
      'id' => 'form-form',
      'enableAjaxValidation' => false,
        //This is very important when uploading files
      'htmlOptions' => array('enctype' => 'multipart/form-data'),
    ));
  ?>    
    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username'); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>
    <!-- Other Fields... -->
    <div class="row">
        <?php echo $form->labelEx($model,'attachment_photo'); ?>
        <?php
    $this->widget('xupload.XUpload', array(
        'url' => Yii::app()->createUrl("form/uploadAdditional", array("parent_id" => 1)),
        'model' => $model,//An instance of our model
        'attribute' => 'file',
        'multiple' => true,
        //Our custom upload template
        'uploadView' => 'application.views.site.upload',
        //our custom download template
        'downloadView' => 'application.views.site.download',
        'options' => array(//Additional javascript options
            //This is the submit callback that will gather
            //the additional data  corresponding to the current file
            'submit' => "js:function (e, data) {
                var inputs = data.context.find(':input');
                data.formData = inputs.serializeArray();
                return true;
            }"
        ),
    ));
    ?>

3 个答案:

答案 0 :(得分:0)

重要的是你的$ _POST数组看起来像什么。 (用Firebug或Fiddler检查)。

你的$ _POST应该是这样的:

array (
  'form-form' => array ( /* stuff for additional form data */)),
  'XupLoad' => array ( /* stuff pertaining to XuLoad */)),
)

通过这种方式,附加表单数据与XupLoad数据分开。 Xupload扩展中包含的操作处理数组的$_POST['XupLoad']部分。您必须编写代码来处理数组的$_POST['form-form']部分。

顺便说一句,Blueimp的jQuery文件上传有一个新版本,其中包含重要的改进和错误修复。它看起来不像XupLoad扩展包含这个最新版本。

我建议使用Blueimp的最新版本。它有一个UploadHandler类,负责服务器端的所有事务。最新版本写得非常好。您仍然需要添加用于处理其他表单数据的代码...

事后补充:看起来你正在嵌套你的小部件。我不确定这是否可行。检查浏览器中的页面源。确保生成的HTML是健全的。可能是你正在生成嵌套表单,我怀疑它们是如何工作的。

答案 1 :(得分:0)

这是我的实时测试网站。 http://vincentlkl.allalla.com/我已经输入mulitpart的表格

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form-form',
'enableAjaxValidation'=>false,
'stateful'=>true, 
'htmlOptions'=>array('enctype' => 'multipart/form-data')

)); ?&GT;

/ fileupload小部件 /

<?php $this->widget('bootstrap.widgets.TbFileUpload', array(
'url' => Yii::app()->createUrl("form/upload"),
        'model' => $model,
        'attribute' => 'file',
        'multiple' => true,
        'options' => array(
            'maxFileSize' => 3000000,
            'acceptFileTypes' => "js:/(\.|\/)(jpe?g|png)$/i",
        )

));
 ?>

我无法在表单中上传文件。

答案 2 :(得分:0)

目前,忘掉你打电话的小部件。下面是应该生成的HTML示例,以便将jQuery FileUpload与其他表单字段组合:

<!-- The file upload form used as target for the file upload widget -->
<form id="fileupload" action="" method="POST" enctype="multipart/form-data">
        <input name="Input_upload[titleInArchive]" id="Input_upload_titleInArchive" type="text" />   

    <div class="row fileupload-buttonbar">
        <div class="span7">
            <!-- The fileinput-button span is used to style the file input field as button -->
            <span class="btn btn-success fileinput-button">
                <i class="icon-plus icon-white"></i>
                <span>Add files...</span>
                <input type="file" name="files[]" multiple>
            </span>
            <button type="submit" class="btn btn-primary start">
                <i class="icon-upload icon-white"></i>
                <span>Start upload</span>
            </button>
    <!-- the rest of the demo jQuery File upload page ... -->
</form>

此表单有一个输入字段 titleInArchive ,它与jQuery文件上载表单共存。 <input>位于jQuery文件上载标记内。因此,在您的情况下,您会将额外的<input>放在$this->widget('xupload.XUpload', ...)

完成此操作后,点击开始上传按钮。在Firebug中,检查XHR请求。 $ _POST数组是什么样的?在你的情况下,它应该像

$_POST['Form']['username'] ...
$_POST['Form']['identity_card'] ...    

我希望我有意义......