【问题标题】:Form data on load of Blueimp jQuery File Upload加载 Blueimp jQuery File Upload 时的表单数据
【发布时间】:2012-12-10 16:42:12
【问题描述】:

似乎我到处寻找并尝试了一切,但我无法让它工作,任何帮助将不胜感激!我正在使用 Blueimp 的 jQuery 文件上传。我已通过以下方式更改了 UploadHandler.php,以使此上传适用于我系统中的多个记录和位置。我正在使用 PHP 和 MySQL。

添加到handle_file_upload函数,以便将文件路径、文件名、文件类型、文件大小、记录类型和“作业号”上传到我在MySQL中为每个不同文件创建的表中。

$file->upload_to_db = $this->add_file($job_num, $recordtype, $file_path, $filename, $type, $file_size);

此作业编号作为获取变量发送到页面(与文件上传)以及添加文件时发送到处理程序的隐藏表单字段。我添加的add_file函数如下:

function add_file($job_num, $recordtype, $filepath, $filename, $filetype, $filesize)
{
    $filepath = addslashes($filepath);
    $insert_query = $this->query("INSERT INTO fileupload (job_num, recordtype, file_path, file_name, file_type, file_size) VALUES ('".$job_num."','".$recordtype."','".$filepath."','".$filename."','".$filetype."','".$filesize."')");
    return $insert_query;
}

查询功能:

protected function query($query) {
    $database = $this->options['database'];
    $host = $this->options['host'];
    $username = $this->options['username']; 
    $password = $this->options['password'];
    $Link = mysql_connect($host, $username, $password);
    if (!$Link){
        die( mysql_error() );
    }
    $db_selected = mysql_select_db($database);
    if (!$db_selected){
        die( mysql_error() );
    }
    $result = mysql_query($query);
    mysql_close($Link);
    return $result;
}

我也有删除功能。现在,这一切都很好。我添加的每个文件都被保存,路径存储在数据库中。从这里我必须找到一种方法来只显示为该记录上传的文件,而不是显示每条记录的所有文件。我修改了get函数:

public function get($print_response = true) {
      if ($print_response && isset($_GET['download'])) {
        return $this->download();
    }
    $uploads = $this->query_db();
    return $this->generate_response($uploads, $print_response);
}

query_db 函数:

public function query_db() {
    $uploads_array = array();
    // error_log("Job Num: ".$this->job_num."\n\n",3,"error_log.txt");
    $select_result = $this->query("SELECT * FROM fileupload WHERE job_num=423424");
    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 = "filepath_to_url/".$query_results->file_name;
        $file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
        $file->delete_url = "";
        $file->delete_type = "DELETE";
        array_push($uploads_array,$file);
    }
    return $uploads_array;
}

文件路径在我的系统上正确的位置。这又一次,工作得很好。但正如您从 query_db 函数中的查询中看到的那样,我对 job_num 进行了硬编码。在加载第一页时,我需要一种方法来获取它。我一直在寻找一种方法来做到这一点,但没有任何效果。当我提交/添加其他文件时,我可以使用 $_REQUEST['job_num'] 来获取它,但不是在页面加载时。我对 OOP PHP 不是很熟悉,所以其中一些对我来说是新的。

【问题讨论】:

  • 我不确定我是否在关注这个问题,因为您似乎发布了一堆工作代码并且并没有真正关注什么不起作用。据我所知,您的工作编号为$job_num。为什么不直接将这个值传递给query_db() 函数?
  • $job_num 在 handle_file_upload 函数中。该变量仅在上传文件时设置。为了设置该变量,我使用了传递给类的隐藏表单字段中的 $_REQUEST['job_num']。我不知道在实际加载页面时如何将其传递给类。我发布了很多工作代码,因为我所拥有的一切都工作,它只是硬编码。我需要一种在页面首次加载时传递它的方法,但我不知道该怎么做。

标签: php jquery file-upload blueimp


【解决方案1】:

根据您的 cmets,您可能希望将 job_number 属性添加到您的类中。当你像这样调用add_file() 时,你可以在你的类中设置这个值

function add_file($job_num, $recordtype, $filepath, $filename, $filetype, $filesize)
{
    $this->job_number = $job_num;
    $filepath = addslashes($filepath);
    $insert_query = $this->query("INSERT INTO fileupload (job_num, recordtype, file_path, file_name, file_type, file_size) VALUES ('".$job_num."','".$recordtype."','".$filepath."','".$filename."','".$filetype."','".$filesize."')");
    return $insert_query;
}

然后您可以在您的 query_db() 方法中引用此工作编号,如下所示:

public function query_db() {
    $uploads_array = array();
    // error_log("Job Num: ".$this->job_num."\n\n",3,"error_log.txt");
    $select_result = $this->query("SELECT * FROM fileupload WHERE job_num=" . $this->job_number);
    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 = "filepath_to_url/".$query_results->file_name;
        $file->thumbnail_url = "filepath_to_thumbnail/".$query_results->file_name;
        $file->delete_url = "";
        $file->delete_type = "DELETE";
        array_push($uploads_array,$file);
    }
    return $uploads_array;
}

请注意,我的代码示例中没有考虑数据卫生。您还需要这样做以防止 SQL 注入。实际上,您应该真正考虑使用 mysqli_* 函数,因为 mysql_* 已被弃用。您还应该将数据库连接传递到您的类中,您可以将它用于类的所有区域(例如 add_file 方法,在将作业编号设置到对象之前最好转义它)。

【讨论】:

  • 如何将 $job_num 属性添加到类中?在 index.php(与 UploadHandler.php 相同的目录)中,需要处理程序和 $upload_handler = new UploadHandler();我不知道这个页面是如何被调用的,但是当我注释掉这两行时,没有任何效果。我尝试将它们移动到上面有文件上传的脚本,以便我可以执行 new UploadHandler($job_num) 但这不起作用。我收到“未选择数据库”错误。
  • 我在文件中搜索了 index.php,但找不到它的位置,但不知何故它被调用了,这就是创建对象的位置。那么如果我不能在 index.php 中添加属性,我该如何添加属性
  • 你说你在这个类中添加了新方法,对吧?您在哪里添加了这些方法并复制并粘贴了您发布的代码?该类是您要添加附加属性的地方。
  • 我想我不确定您所说的财产是什么意思。我有 $upload_handler = new UploadHandler($job_num);以及在 __construct 中。但是 index.php 中的 $job_num 是空的,因为我不明白 blueimp 是如何设置的。
  • I 属性是存储在类中的变量。如果您已经将 $job_num 传递给构造函数,只需在构造函数中设置 $this->job_number 并在该类实例中需要的地方引用它。我对 BlueImp 或您正在使用的东西一无所知,而且您还没有在 index.php 中显示代码,以便人们可能了解它是如何填充的。
【解决方案2】:

也许这会有所帮助

protected function get_file_objects() {

    $user_id = $this->options['session_id'];
    $files = $this->query("SELECT yourname FROM yourname WHERE yourname='$user_id'");
    $files_array=array();
    while($row = mysql_fetch_assoc($files)){
        array_push($files_array,$row['yourname']);
    }
    return array_values( array_filter( array_map(
        array($this, 'get_file_object'),
        $files_array
    ) ) );
}

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    相关资源
    最近更新 更多