2015-05-09 85 views
1

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

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}); 
}); 

回答

0

我发现自己同样的问题,并做了一些研究,我没有发现,在文档任何东西,所以我做了解决方法。

基本上我研究了它发送到前端的库被配对的对象,我重写了我的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}); 
     }); 

    });