【问题标题】:jQuery File Upload custom get_file_objects function not working to import data from databasejQuery File Upload 自定义 get_file_objects 函数无法从数据库导入数据
【发布时间】:2018-06-21 02:19:47
【问题描述】:

我已经扩展了 jquery 插件 blueimp / jQuery-File-Upload 的 UploadHandler 类。我有一个自定义的 handle_file_upload 函数,如下所示。

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
        $index = null, $content_range = null) {
    $file = parent::handle_file_upload(
        $uploaded_file, $name, $size, $type, $error, $index, $content_range
    );
    $file->unique_code = random_string(32);
    $file->files_locations = $this->get_file_objects();
    $file->files_locations_parent = parent::get_file_objects();
    if($file->files_locations === $file->files_locations_parent):
        $file->files_same = 'Yes';
    elseif($file->files_locations == $file->files_locations_parent):
        $file->files_same = 'Nearly';
    else:
        $file->files_same = 'No';
    endif;
    $file->email_id = $_REQUEST['email_id'];
    if (empty($file->error)) {
        $email_date = array();
        $email_date['filename'] = $file->name;
        $email_date['size'] = $file->size;
        $email_date['type'] = $file->type;
        $email_date['unique_code'] = $file->unique_code;

        db_insert($this->table, $email_date);
    }
    return $file;
}

这个类和函数运行良好,但是当我尝试引入一个自定义的 get_file_objects 函数时,它行为不端。

protected function get_file_objects($iteration_method = 'get_file_object') {
    $upload_dir = $this->get_upload_path();
    if (!is_dir($upload_dir)) {
        return array();
    }
    return array_values(array_filter(array_map(
        array($this, $iteration_method),
        // $this->get_filenames($_REQUEST['email_id'])
        scandir($upload_dir)
    )));
}

正如上面打印的那样工作正常,但这只会返回与父类相同的内容。当我注释掉 scandir 并取消注释另一个时,我没有显示任何文件。

我知道 get_file_objects 函数正在工作,因为我将输出附加到 handle_file_upload 函数中的文件对象,然后在客户端的控制台中显示它。这是代码sn-p和输出。

$file->files_locations = $this->get_file_objects();
$file->files_locations_parent = parent::get_file_objects();
if($file->files_locations === $file->files_locations_parent):
    $file->files_same = 'Yes';
elseif($file->files_locations == $file->files_locations_parent):
    $file->files_same = 'Nearly';
else:
    $file->files_same = 'No';
endif;

由于数据库中已知的不同文件与文件系统相对应,对象并不相同,但我相信结构是相同的:

files_locations
:
Array(1)
0
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.41.png"
name
:
"Screen Shot 2018-01-08 at 15.44.41.png"
size
:
14246
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
__proto__
:
Object
length
:
1
__proto__
:
Array(0)
files_locations_parent
:
Array(4)
0
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.03.png"
name
:
"Screen Shot 2018-01-08 at 15.44.03.png"
size
:
107418
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.03.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.03.png"
__proto__
:
Object
1
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.41.png"
name
:
"Screen Shot 2018-01-08 at 15.44.41.png"
size
:
14246
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
__proto__
:
Object
2
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.45%20%281%29.png"
name
:
"Screen Shot 2018-01-08 at 15.44.45 (1).png"
size
:
14230
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.45%20%281%29.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.45%20%281%29.png"
__proto__
:
Object
3
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.45.png"
name
:
"Screen Shot 2018-01-08 at 15.44.45.png"
size
:
14230
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.45.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.45.png"
__proto__
:
Object
length
:
4
__proto__
:
Array(0)

启动文件上传器的Javascript代码如下:

$('#email-form').fileupload({
    url: HOME + 'email_attachment.php',
    formData: {email_id: email_id}
});

其中 email_id 在 WHERE 子句中用于从服务器中选择所需的附件。

任何关于为什么这对我不起作用的帮助将不胜感激。

谢谢,

斯图。

【问题讨论】:

    标签: javascript php jquery file-upload jquery-file-upload


    【解决方案1】:

    我找到了解决方案。问题不在于 PHP,而在于 JavaScript。当在 JS 中初始化 fileupload 时,它似乎没有发送 formData,这只发生在文件上传发生时。因此,我只是更改了向服务器提交数据的方式,而不是使用 formData 和相应的 $_REQUEST,而是创建了一个 Get 调用和 $_GET。代码如下。

    $('#email-form').fileupload({
        url: HOME + 'email_attachment.php?email_id=' + email_id
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-22
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多