doutou3725 2014-12-01 16:30
浏览 103

Yii使用Ajax上传文件 - 传递数据数组(formData)

I have been bashing away at this for a while and seem to be advancing small steps at a time. I need to offer a way for users to upload their files via Ajax. I have seen that formData needs to be used in order to pass the file itself. Using these two questions as a reference: Uploading file in Yii using ajax Yii ajax file upload

I have the following code on my view page:

<?php
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'post-form',

    'htmlOptions' => array('enctype' => 'multipart/form-data'),
    ));


echo '<div class="columns small-5">' . $form->fileField($upload, 'image') . '</div>';
echo $form->error($upload, 'image');
echo CHtml::button("(+)",array('title'=>"Topic",'onclick'=>'js:upload_file(this);')); 


$this->endWidget(); ?>

<script>
function upload_file(){
var fd = new FormData();
var e = document.getElementById("UploadedFiles_image");
fd.append( "UploadedFiles[image]", $(e)[0].files[0]);

$.ajax({
    url: 'UploadInstallPhoto',
    type: 'POST',
    cache: false,
    data: fd,
    datatype: 'json',
    processData: false,
    contentType: false,
    success: function (data) { 

    },
    error: function () {
        alert("ERROR in upload");
    }
});
}
</script>

I have the following code in my controller:

    if(Yii::app()->request->isAjaxRequest)
    {

        $upload = new UploadedFiles;

        $upload->attributes = $_POST['UploadedFiles'];

        $file = CUploadedFile::getInstance($upload,'image');

        $pi = pathinfo($file);

        $upload->name = $pi['filename'];
        $upload->ext = $pi['extension'];
        $upload->acc_id = $id;
        $upload->type_id = 1;
        $upload->date_uploaded = date('Y-m-d');
        if($upload->save())
        {
            $file->saveAs(Yii::app()->params['uploadFiles'] . $upload->id . '.' . $pi['extension']);
            Yii::app()->user->setFlash('success',Yii::t('flash','flash.accounts.successful_upload'));
        }

    }

With the above code I am now able to do ajax file submissions and this is working as expected. The issue I am now having is that the file is placed into a folder and an entry in the database is created. In the DB I reference the persons account so the file can be seen on the account for that user.

What I need to be able to do is to pass an array of data with the ajax POST i.e

data: {file: fd, id: id}

However, when I alter the line to use an array no data is posted. Please can someone suggest a potential fix or how I could pass multiple values including the file value.

  • 写回答

1条回答 默认 最新

  • dongqie8661 2014-12-02 08:36
    关注

    Here is the solution that worked for me. I am now able to upload a file via Ajax and also pass multiple variables as well as the file.

    Here is my code for the View page:

    <?php
        $form=$this->beginWidget('CActiveForm', array(
            'id'=>'post-form',
            'htmlOptions' => array('enctype' => 'multipart/form-data'),
            ));
    
    
        echo '<div class="columns small-5">' . $form->fileField($upload, 'image') . '</div>';
        echo $form->error($upload, 'image');
        echo CHtml::button("(+)",array('title'=>"Topic",'onclick'=>'js:upload_file(this);')); 
    
    
        $this->endWidget();
    ?>
    
    <script>
    function upload_file(){
        var fd = new FormData();
        var e = document.getElementById("UploadedFiles_image");
        fd.append( "UploadedFiles[image]", $(e)[0].files[0]);
        fd.append( "UploadedFiles[acc_id]", <?php echo $model->account_id; ?>); //This lines appends the required ID, thanks to Adeneo
    
        $.ajax({
            url: 'UploadInstallPhoto',
            type: 'POST',
            cache: false,
            data: fd,
            datatype: 'json',
            processData: false,
            contentType: false,
            success: function(data){ 
    
            },
            error: function(){
                alert("ERROR in upload");
            }
        });
    }
    </script>
    

    Here is my Controller code:

    public function actionUploadInstallPhoto()
    {
        if(Yii::app()->request->isAjaxRequest)
        {
            $upload = new UploadedFiles;
    
            $upload->attributes = $_POST['UploadedFiles'];
    
            $file = CUploadedFile::getInstance($upload,'image');
    
            $pi = pathinfo($file);
    
            $upload->name = $pi['filename'];
            $upload->ext = $pi['extension'];
            $upload->acc_id = $_POST['UploadedFiles']['acc_id'];
            $upload->type_id = Yii::app()->params['installPhoto'];
            $upload->date_uploaded = date('Y-m-d');
            if($upload->save())
            {
                $file->saveAs(Yii::app()->params['uploadFiles'] . $upload->id . '.' . $pi['extension']);
                Yii::app()->user->setFlash('success',Yii::t('flash','flash.accounts.successful_upload'));
            }
    
        }
    }
    

    Hopefully this will help someone in the same position as I was.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)