5

已编辑

使用jQuery 文件上传文档,我可以将数据与 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 数据后出现意外的非空白字符”,我对此进行了谷歌搜索,并找到了信息,但没有什么对我有帮助。

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

图片图2

4

1 回答 1

2

这将为您提供从 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回调来获取数据并使用它。

于 2013-09-07T16:31:30.983 回答