简体   繁体   English

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

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

It seems that I have looked everywhere and tried everything and I can not get this to work, any help would be much appreciated! 看来我到处都是,尝试了一切,但我无法使它正常工作,任何帮助将不胜感激! I am using Blueimp's jQuery File Upload. 我正在使用Blueimp的jQuery文件上传。 I have changed the UploadHandler.php in the following ways to make this upload work with multiple records and places within my system. 我已通过以下方式更改了UploadHandler.php,以使该上载可以与系统中的多个记录和位置一起使用。 I am using PHP and MySQL. 我正在使用PHP和MySQL。

Added to handle_file_upload function so that the file path, file name, file type, file size, the record type, and the "job number" is uploaded to a table I created in MySQL for each different file. 添加到handle_file_upload函数,以便将文件路径,文件名,文件类型,文件大小,记录类型和“作业号”上载到我在MySQL中为每个不同文件创建的表中。

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

This job number is sent to the page (with the file upload) as a get variable as well as a hidden form field to send to the Handler when adding a file. 此作业号作为get变量发送到页面(带有文件上传),并在添加文件时发送给Handler的隐藏表单字段。 The add_file function I added is as follows: 我添加的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;
}

The query function: 查询功能:

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

I have a function for the delete as well. 我也有删除功能。 Now, this all works fine. 现在,一切正常。 Every file I add is saved and the path is stored in the database. 我添加的每个文件都会保存,路径会存储在数据库中。 From here I had to find a way to only show the files uploaded for that record instead of showing all files for every record. 从这里,我必须找到一种方法,仅显示该记录上载的文件,而不是显示每个记录的所有文件。 I modified the get function: 我修改了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);
}

The query_db function: 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;
}

Where the filepath is correct on my system. 文件路径在我的系统上正确的位置。 This, again, works just fine. 再次,这很好。 But as you can see from the query in the query_db function I have the job_num hard coded. 但是正如您从query_db函数中的查询可以看到的那样,我对job_num进行了硬编码。 I need a way to get this when the first page is loaded. 我需要一种在第一页加载时获取此信息的方法。 I have looked for a way to do this and nothing has worked. 我一直在寻找一种方法来执行此操作,但没有任何效果。 When I submit/add other files I can use the $_REQUEST['job_num'] to get it, but not when the page is loaded. 当我提交/添加其他文件时,我可以使用$ _REQUEST ['job_num']来获取它,但是当页面加载时却不能。 I am not very familiar with OOP PHP so some of this is new to me. 我对OOP PHP不太熟悉,所以其中一些对我来说是新的。

Based on your comments, you would probably want to add a job_number property to your class. 根据您的评论,您可能希望向您的班级添加一个job_number属性。 You could then set this value in your class when you call add_file() like this 然后,当您像这样调用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;
}

You could then reference this job number in your query_db() method like this: 然后,您可以像这样在您的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;
}

Note I did not take data sanitation into account in my code samples. 注意我的代码示例中没有考虑数据卫生。 You would need to do that as well to prevent against SQL injection. 您还需要这样做以防止SQL注入。 In reality, you should really look at using the mysqli_* functions as the mysql_* are deprecated. 在现实中,你应该看看使用mysqli_*用作mysql_*已被弃用。 You should also probably pass the database connection into you class you you have it available to all areas of the class (like the add_file method where it would be nice to escape the job number before setting it in the object). 您可能还应该将数据库连接传递到您的类中,并且您可以将其用于该类的所有区域(例如add_file方法,最好在对象中进行设置之前先转义作业号)。

Maybe this can be helpful 也许这会有所帮助

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