我将图像作为longBLOB存储在database.my表结构中,如下所示
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
`body` text,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`image` longblob,
PRIMARY KEY (`id`)
).
我需要发送此图像与所有其他数据的json格式。我在控制器中的代码是
public function index()
{
$this->set(array('posts'=> $this->Post->find('all'),
'_serialize' => array('posts')));
}
当我转到localhost/app/posts.json时,我得到了json格式的所有数据,但对于image,我得到的却是null。
{"Post":{"id":"1","title":"The title","body":"This is the post body.","created":"2013- 11-11 16:32:45","modified":null,"user_id":"1","image":null}
我已经找到了一些解决方案,说明了在数据库中存储图像路径,并将其转换为JSON格式并发送到服务器,但我不想在数据库中存储图像路径,所以有人可以帮助我发送JSON格式的图像吗?
发布于 2013-12-06 21:15:29
我不会在数据库中存储图像,但是,好吧,由您选择。
使用base64对二进制日期进行编码,有关示例,请参阅Binary Data in JSON String. Something better than Base64。
https://stackoverflow.com/questions/20423225
复制相似问题