ajax 文件上传 所有文件通过一个上传地址处理,该怎么处理

新手提问 · zhenhua2340 · 于 6年前 发布 · 5314 次阅读

使用 Bootstrap File Input 插件上传图片

使用 ajax 异步上传,

希望所有上传的图片全部使用一个上传地址

服务端的代码不知怎么处理 ?

备注: 不想使用 最原始的 $_FILES 来处理,

视图:

<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">缩略图:</label>
    <div class="col-sm-10">
        <input id="input-700" name="Articles[thumb]" type="file" multiple >
    </div>
</div>
<script>
    $("#input-700").fileinput({
        language: "zh",
        allowedFileExtensions : ['jpg', 'jpeg', 'png','gif'],
        uploadUrl: "<?= Url::toRoute('/upload/index');?>", // server upload action
        uploadAsync: true,
        maxFileSize: 1000,
        maxFileCount: 5
    });
</script>

控制器:

    public function actionIndex()
    {
        $response = [];

        if (Yii::$app->request->isPost) {

            $model = new UploadForm();

            $model->images = UploadedFile::getInstance($model, 'thumb');

            $response['images'] = $model->images;
            $response['url'] = $model->upload();
            $response['id']  = Yii::$app->request->post('id');
            $response['file'] = $_FILES;
            $response['post'] = Yii::$app->request->post();

        }
        echo Json::encode($response);
    }

关键点 主要是

$model->images = UploadedFile::getInstance($model, 'thumb'); 中的2个参数 $model, 'thumb'

没搞懂,不知道怎么写,

目前 $model->images 为 NULL

求指点 。。。

共收到 2 条回复
forecho#16年前 0 个赞

你先再控制器这边打印 $_FILES 的值

zhenhua2340#26年前 0 个赞

@forecho #1楼 $_FILES 和 post 的值时这样的:

"file": {
    "Articles": {
        "name": {
            "images": "shaba01.png"
        },
        "type": {
            "images": "image/png"
        },
        "tmp_name": {
            "images": "/tmp/php2mRF4G"
        },
        "error": {
            "images": 0
        },
        "size": {
            "images": 60006
        }
    }
},
"post": {
    "file_id": "0"
}
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册