jQuery文件从数据库上传读取文件

时间:2015-05-09 10:57:44

标签: php jquery-file-upload

我正在尝试为jQuery File Upload插件创建自定义类。我成功地将数据插入到我的数据库中,但是我无法从数据库中读取文件。由于我不熟悉面向对象编程,所以我无法弄清楚问题出在哪里。此外,没有足够的文档。

class CustomUploadHandler extends UploadHandler {

    public function get($print_response = true) {

        $db = new DB;
        $query = $db->get_rows("SELECT * FROM `files` ORDER BY `name`");

        foreach ($query as $row)
            {   
                $file = new stdClass();
                $file->id = $row->id;
                $file->name = $row->name;
                $file->size = $row->size;
                $file->type = $row->type;
                $file->title = $row->title;
                $file->url = $row->url;
                $file->thumbnail_url = $row->thumbnail;
                $file->delete_url = "";
                $file->delete_type = "DELETE";
             }

         return $this->generate_response($query, $print_response);
    }

}

我的js文件中也有这个:

// Load existing files:
$('#fileupload').addClass('fileupload-processing');
$.ajax({
    // Uncomment the following to send cross-domain cookies:
    //xhrFields: {withCredentials: true},
    url: $('#fileupload').fileupload('option', 'url'),
    dataType: 'json',
    context: $('#fileupload')[0]
}).always(function () {
    $(this).removeClass('fileupload-processing');
}).done(function (result) {
    $(this).fileupload('option', 'done')
        .call(this, $.Event('done'), {result: result});
});

1 个答案:

答案 0 :(得分:0)

我发现自己遇到了同样的问题,经过一些研究后我没有在文档中找到任何内容,所以我做了一个解决方法。

基本上我研究了它发送到前面的库是如何被maped的对象,我从我的CustomHandler中的处理程序重写了我的index.php中的get函数。

我发现的反对意见是,当我不需要时,我开始在我的数据库中保存图像的大小,但无论如何都能正常工作。

 public function get($print_response = true) {
    if ($print_response && $this->get_query_param('download')) {
        return $this->download();
    }
    //this is my filter for the data the post_id
    //see the main.js to see how i send it
    $postId = $_GET['post_id'];
    $arrayObjects = array();
    $sql = 'SELECT `id`, `name`, `size` ,`post_id`, `path_img`, `descripcion` FROM `'
        .$this->options['db_table'].'` WHERE `post_id`=?';
    $query = $this->db->prepare($sql);
    $query->bind_param('i', $postId);
    $query->execute();
    $query->bind_result(
        $id,
        $name,
        $size,
        $post_id,
        $path_img,
        $descripcion
    );

    // in $this->base_url i have the http:// value from my host
    while ($query->fetch()) {
        $arrayObjects[] = (object) array("name"=>$name,
            "size"=>$size,
            "url"=> $this->base_url.$path_img."/".$name
        ,"thumbnailUrl"=> $this->base_url.$path_img."/thumbnail/".$name
        ,"deleteUrl"=>$this->base_url."galeria/server/php/index.php?file=".$name."&_method=DELETE"
        ,"deleteType"=>'POST');

    }

    /* this is the object that you need to send to the front
     * [name] => Captura de pantalla 2016-06-30 a las 4.40.10 p.m..png
     * [size] => 61921 [url] => http://localhost/papmendoza/wp-content/uploads/galeria/Captura%20de%20pantalla%202016-06-30%20a%20las%204.40.10%20p.m..png
     * [thumbnailUrl] => http://localhost/papmendoza/wp-content/uploads/galeria/thumbnail/Captura%20de%20pantalla%202016-06-30%20a%20las%204.40.10%20p.m..png
     * [deleteUrl] => http://localhost/papmendoza/galeria/server/php/index.php?file=Captura%20de%20pantalla%202016-06-30%20a%20las%204.40.10%20p.m..png&_method=DELETE
     * [deleteType] => POST )
     *
     */


    $response = array(
            $this->options['param_name'] => $arrayObjects
        );

    return $this->generate_response($response, $print_response);
}

在我的main.js中,我有类似的东西

// i have a select in my custom form and i need the file uploader change
//with my select so here i basically do that
     $('#noticia').on('change', function () {
        var post_id = this.value; // or $(this).val()
        $('#fileupload').addClass('fileupload-processing');
        $.ajax({
            url: $('#fileupload').fileupload('option', 'url') + '?post_id=' + post_id,
            dataType: 'json',
            context: $('#fileupload')[0]
        }).always(function () {
            $(this).removeClass('fileupload-processing');
        }).done(function (result) {
              //you need to remove the files first
             $("#fileupload").find(".files").empty();
            $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
        });

    });