【问题标题】:jQuery File Upload, get and edit Data which is saved in DBjQuery 文件上传、获取和编辑保存在数据库中的数据
【发布时间】:2013-09-11 13:46:08
【问题描述】:

已编辑

使用jQuery File Upload Documentation,我可以将数据与图片一起保存在 MySQL DB 中 (见图1+2)

现在我陷入困境的是如何取回数据。上传后,我想用数据库中的数据显示图片,但我找不到如何做到这一点。

如果有人知道如何编辑附加数据库数据的 json,或者我如何 var_dump 它只是为了将我推向正确的方向,那就太棒了!我找不到创建 json 的位置。 json 现在看起来像这样:

{"files":[{
    "name"         : "02 (1).jpg",
    "size"         : 12508,
    "type"         : "image\/jpeg",
    "url"          : "http:\/\/localhost:8888\/server\/php\/files\/02%20%281%29.jpg",
    "thumbnailUrl" : "http:\/\/localhost:8888\/server\/php\/files\/thumbnail\/02%20%281%29.jpg",
    "deleteUrl"    : "http:\/\/localhost:8888\/server\/php\/?file=02%20%281%29.jpg",
    "deleteType"   : "DELETE"
}]}

我想让它像:

{"files":[{
    "name"         : "02 (1).jpg",
    "size"         : 12508,
    "type"         : "image\/jpeg",
    "url"          : "http:\/\/localhost:8888\/server\/php\/files\/02%20%281%29.jpg",
    "thumbnailUrl" : "http:\/\/localhost:8888\/server\/php\/files\/thumbnail\/02%20%281%29.jpg",
    "deleteUrl"    : "http:\/\/localhost:8888\/server\/php\/?file=02%20%281%29.jpg",
    "deleteType"   : "DELETE",
    "titleDB"      : "Title1",
    "textDB"       : "Lorem ipsum dolor...."
}]}

我尝试了(就像 rAjA 解释的那样)并更改了以下内容

require('UploadHandler.php');
$upload_handler = new UploadHandler();
$response_enc = $this->upload_handler->initialize();

但是我收到一条错误消息“JSON.parse:JSON 数据后出现意外的非空白字符”,我对此进行了谷歌搜索,并找到了信息,但没有任何帮助。

谁能帮我解决这个问题或知道我在哪里可以找到信息?谢谢!

【问题讨论】:

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


    【解决方案1】:

    这将为您提供从 uploadhandler 库编辑 json 响应的基本概念,

    已编辑:

    在您的 uploadhandler.php 库中并更改此行,使其返回响应,

    public function post($print_response = true)public function post($print_response = false)

    public function get($print_response = true)public function get($print_response = false)

    protected function initialize()public function initialize()

    已编辑:也改一下function __construct($options = null, $initialize = false, $error_messages = null)

    function initialize()
    //
                case 'POST':
                    return $this->post(); //Change this line like here
                    break;
    ////
                case 'GET':
                    return $this->get(); //Change this line like here
                    break;
    //
    

    然后在调用库的函数中获取响应,

    require('UploadHandler.php');
    $upload_handler = new UploadHandler();
    $response = $upload_handler>initialize();
    
    print_r($response); //Dump the response here and check    
    
    $custom_arr = //Save all your custom variables here (DB insert id, Text etc)
    //Sample format of custom_arr
    //$custom_arr['insert_id']  = $mysql_primary_key_id;
    //$custom_arr['txt']  = $user_custom_text_field;
    //
    
    $response['custom_data'] = $custom_arr;
    
    echo json_encode($response);
    

    在您的前端,您可以使用fileuploaddone 回调来获取数据并使用它。

    【讨论】:

    • 一旦我有了这个 $response_enc = $this->upload_handler->initialize();我收到一个错误(JSON.parse:JSON 数据后出现意外的非空白字符)知道为什么吗? $custom_arr(变量)应该是什么样子?
    • 这行引起的还是一样的错误 $response_enc = $this->upload_handler->initialize();还需要更多信息如何使用 fileuploaddone(文档中没有太多内容)我应该更新我的问题吗?
    • 你有没有像我提到的那样初始化函数中的 chang?可以显示你遇到的错误吗?
    • 我刚刚看到您的编辑。您能否确认您已像我编辑的代码一样对代码进行了更改?因为如果你删除了json_decode 行,你不应该得到JSON.parse: unexpected 错误
    • 如果你已经成功编辑了Json响应,我们可以专注于在前端代码中使用fileuploaddone处理它
    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 2015-05-25
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多