繁体   English   中英

与数据库集成的Blueimp jQuery文件上传

[英]Blueimp jQuery File Upload Integrated with Database

此插件在页面加载时读取blueimproot / server / php / files上的图像文件。 我需要从数据库中读取记录,并用我的自定义结构替换“下载” HTML结构。 我想显示目录产品,哪些项目受到通过此插件上传/删除图像的影响。

到目前为止我做到了这一点:

  • 我更改了blueimproot / server / php / upload.class.php中的 public function get() { ... } ,以从数据库中检索记录。 此函数返回json对象。

     public function get() { /* default code of Blueimp $file_name = isset($_REQUEST['file']) ? basename(stripslashes($_REQUEST['file'])) : null; if ($file_name) { $info = $this->get_file_object($file_name); } else { $info = $this->get_file_objects(); } header('Content-type: application/json'); echo json_encode($info); */ include_once('../../../../connection.php'); $id_cat = $_REQUEST['catid']; $query = "SELECT id, name, price, img_path FROM products WHERE id_cat = $id_cat ORDER BY id"; $prods = mysql_query($query); $prod_arr = array(); while($prod = mysql_fetch_assoc($prods)) { $prod_arr[] = $prod; } header('Content-type: application/json'); echo json_encode($info); } 
  • 我发现从blueimproot / server / php中的 index.php调用了该函数:

     switch ($_SERVER['REQUEST_METHOD']) { ... case 'GET': $upload_handler->get(); break; ... 

    }

我不知道返回的json对象在哪里处理以显示给UI。 已经2天了,仍然无法跟踪该功能流程。 请帮忙。 谢谢。

原始在线演示: http : //blueimp.github.com/jQuery-File-Upload/

原始插件下载: https : //github.com/blueimp/jQuery-File-Upload/downloads

我的建议是在Firebug中打开“ 网络”选项卡,并注意对server/php/index.php任何GET请求。 如果发生在特定事件之后,那么您将对应该去哪里有一个更好的了解。

我确实浏览了源文件,发现的唯一GET请求是在main.js中

$('#fileupload').each(function () {
  var that = this;
  $.getJSON(this.action, function (result) {
    if (result && result.length) {
      $(that).fileupload('option', 'done')
        .call(that, null, {result: result});
        }
    });
  });
}
 public function get() {
    /*
    $file_name = isset($_REQUEST['file']) ?
        basename(stripslashes($_REQUEST['file'])) : null;
    if ($file_name) {
        $info = $this->get_file_object($file_name);
    } else {
        $info = $this->get_file_objects();
    }
    header('Content-type: application/json');
    echo json_encode($info);
    */
        $id_cat = $_REQUEST['catid'];
        $query = "SELECT id, name, price, img_path FROM products WHERE id_cat = $id_cat ORDER BY id";
        $prods = mysql_query($query);

        $prod_arr = array();
        while($prod = mysql_fetch_assoc($prods)) {
            //$prod_arr[] = $prod;

            $file = new stdClass();
            $file->name = "";// here image name goes i do not find image name in your select query
            $file->size = filesize($prod["img_path"]);// should be complete path
            $file->url =  $prod["img_path"];// should be relative path (http://localhost/images/234.jpg)
            $file->thumbnail_url = $prod["img_path"]; // thumbnail path
            $this->delete_type = "DELETE";
            $this->delete_url = ""; //here delete url you can delete image from database 
            array_push($prod_arr,$file);
        }
        header('Content-type: application/json');
    echo json_encode($prod_arr);
}

遵循以下WIKI: https : //github.com/blueimp/jQuery-File-Upload/wiki/Working-with-databases

我将上传文件设置为要插入到数据库中,然后按如下所示更改了GET函数:

            public function get() {
                $uploads = $this->query_db();
                header('Content-type: application/json');
                echo json_encode($uploads);
            }

和我的query_db功能如下:

         public function query_db() {
    $uploads_array = array();
    $select_result = $this->query("SELECT * FROM `uploads` ORDER BY `file_name`") or die(mysql_error());
    while($query_results = mysql_fetch_object($select_result))
        {   
            $file = new stdClass();
            $file->id = $query_results->id;
            $file->name = $query_results->file_name;
            $file->size = $query_results->file_size;
            $file->type = $query_results->file_type;
            $file->url = "http://files.domain.com/".$query_results->file_name;
            $file->thumbnail_url = "http://thumbnails.domain.com/".$query_results->file_name;
            $file->delete_url = "";
            $file->delete_type = "DELETE";
            array_push($uploads_array,$file);
        }
    return $uploads_array;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM