繁体   English   中英

加载Blueimp jQuery File Upload时的表单数据

[英]Form data on load of Blueimp jQuery File Upload

看来我到处都是,尝试了一切,但我无法使它正常工作,任何帮助将不胜感激! 我正在使用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);

此作业号作为get变量发送到页面(带有文件上传),并在添加文件时发送给Handler的隐藏表单字段。 我添加的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_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方法,最好在对象中进行设置之前先转义作业号)。

也许这会有所帮助

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

暂无
暂无

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

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