jQuery的文件上传到数据库的base64 [英] Jquery File upload to database in base64

查看:788
本文介绍了jQuery的文件上传到数据库的base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用jQuery文件上传与引导皮肤问题( https://开头github上。 COM / blueimp / jQuery的,文件的上传)。程序本身精美作品原样,即使如在github描述(的 https://github.com/blueimp/jQuery-File-Upload/wiki/Working-with-databases )。

但我真正需要的是将图片保存在Base64格式的数据库。我似乎无法得到它。所以在add_img的功能,我需要以某种方式得到以base64的照片。

  //标准文件上传code
功能add_img($ img_name)
{
    $ add_to_db = $这个 - >查询('。$ img_name插入图片(pic_name,PIC)VALUES(','以base64图片浏览'));
    返回$ add_to_db;
}


解决方案

您的图像转换为使用以​​下code Base64和其存储到数据库。

  $为imageData =的file_get_contents(文件路径/ filename.jpg);
$ base64data = base64_en code($为imageData);

编辑:

  $文件 - >大小= $ FILE_SIZE; //从目前的code$为imageData =的file_get_contents($ FILE_PATH);
$ base64data = base64_en code($为imageData);
$这个 - > add_img($ base64data);

希望这会为你工作。

I'm having trouble using the Jquery File uploader with bootstrap skin (https://github.com/blueimp/jQuery-File-Upload). The program itself works beautifully as is, even with the database which is functional too as described on github (https://github.com/blueimp/jQuery-File-Upload/wiki/Working-with-databases).

But what I really need is to save the picture in the database in base64 format. I can't seem to get it. So in the add_img function i need to somehow get the pic in base64.

// Standard file uploader code
function add_img($img_name)
{
    $add_to_db = $this->query("INSERT INTO pics (pic_name, pic) VALUES ('".$img_name."','picture in base64 here')");
    return $add_to_db;
}

解决方案

convert your image to base64 using following code and store it to db.

$imagedata = file_get_contents("filepath/filename.jpg");
$base64data = base64_encode($imagedata);

EDIT:

$file->size = $file_size;//from current code

$imagedata = file_get_contents($file_path);
$base64data = base64_encode($imagedata);
$this->add_img($base64data);

Hope this will work for you

这篇关于jQuery的文件上传到数据库的base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆