如何使用jquery、ajax和php在数据库中保存图像文件(BLOB)
我想使用jquery、Ajax和PHP将数据库中的图像保存为BLOB文件。 我可以上传它并在表单中显示它(如果它是手动插入数据库的)。但是,当我在表单上更改它时,新图像显示良好,但当我单击“保存”时,它不会保存在数据库中。我在这里没有找到任何使用jquery、Ajax和PHP并将整个图像存储在数据库中的解决方案 以下是我的HTML代码(Content/users.php)的一部分: 以下是我的PHP代码(DataAjax/user.PHP)的一部分:如何使用jquery、ajax和php在数据库中保存图像文件(BLOB),php,jquery,html,ajax,Php,Jquery,Html,Ajax,我想使用jquery、Ajax和PHP将数据库中的图像保存为BLOB文件。 我可以上传它并在表单中显示它(如果它是手动插入数据库的)。但是,当我在表单上更改它时,新图像显示良好,但当我单击“保存”时,它不会保存在数据库中。我在这里没有找到任何使用jquery、Ajax和PHP并将整个图像存储在数据库中的解决方案 以下是我的HTML代码(Content/users.php)的一部分: 以下是我的PHP代码(DataAjax/user.PHP)的一部分: 在postdata中传递错误的图像数据,请在
在postdata中传递错误的图像数据,请在代码中尝试下面的示例 html文件输入:
<input type="file"name="profileImage" id="profileImage" accept=".jpg, .png" />
PHP文件代码:
if (isset($_POST['id'])) {
$postType = $_POST['postType'];
if ($postType == "addOrUpdate") {
$Id = $_POST['id'];
$FullName = $_POST['fullName'];
// chnage this line
$pic = base64_encode(file_get_contents($_FILES['file']));
// or you can send all data in serilize way in database like this
$pic = serialize($_FILES['file']);
$data = new Data();
$db = $data->dataHandler();
$query = "INSERT INTO Users (`id`, `fullname`, `image`)
values ('$Id', '$FullName', '$pic')";
$db->query($query);
}
}
要通过ajax进行文件上载,必须使用formdata对象(其中包括file/blob对象),将其作为数据参数传递给$.ajax,并将processData和contentType设置为false
function addOrUpdateItem(event) {
var itemId = $("#userId").val();
var fullName = $("#txtUserName").val();
var pic = $("#profileImage").prop('files')[0];
var data = new FormData();
data.append('id', itemId);
data.append('postType', addOrUpdate);
data.append('fullName', fullName);
data.append('pic', pic);
$.ajax({
url: 'data_ajax/users.php',
data: data,
type: "POST",
cache: false,
contentType: false,
dataType: false,
success: function(data) {
location.reload();
},
error: function(e) {
alert("error while trying to add or update user!");
}
});
}
您必须使用$\u文件['pic'][tmp\u名称]
来访问该文件
if (isset($_POST['id'])) {
$postType = $_POST['postType'];
if ($postType == "addOrUpdate") {
$Id = $_POST['id'];
$FullName = $_POST['userName'];
$pic = base64_encode(file_get_contents($_FILES['pic'][tmp_name]));
$data = new Data();
$db = $data->dataHandler();
$query = "INSERT INTO Users (`id`, `fullname`, `image`)
values ('$Id', '$FullName', '$pic')";
$db->query($query);
}
}
我没有错。“保存”按钮起作用,它将数据提交到数据库,只有图像字段为空。我完全尝试了你的代码,它不起作用。另一方面,我尝试了另一种解决方案,因为我不想使用
data.append('file',filedata)代码>。因此,我保留了'var itemData={id:itemdid,postType:'addOrUpdate',fullName:fullName,pic:pic};`其中pic是var pic=$(“#profileImage”).val()在PHP代码中:$d1=$\u POST['pic']$d2=$\u文件[$d1]$pic=base64编码(文件获取内容($d2))代码>重试我确实更改了答案请使用:new formdata()和data.append('file',filedata);没有它就没有工作!“filedata”的值是“C:\fakepath\image\u name.png”。“filedata”的值应该是图像的路径吗?为什么是“fakepath”?请尝试添加:[$pic=base64_编码(file\u获取内容($\u FILES['file']['tmp\u name']);$data=mysql\u real\u escape\u字符串($pic);]可能是您获得了图像的真实数据。非常感谢。现在可以了。我用addslashes代替“base64_encode”。代码中有一个小错误:data.append不是file.append。谢谢你的澄清。
var itemId = $("#userId").val();
var fullName = $("#txtUserName").val();
var filedata = $("#profileImage").val();
//Your Formdata Ready To Send
var data = new FormData();
data.append('file', filedata);
data.append('id', itemId);
data.append('fullName', fullName);
$.ajax({
url: 'data_ajax/users.php',
data: data,
type: "POST",
contentType: false,
cache: false,
processData:false,
success: function(data) {
location.reload();
},
error: function(e) {
alert("error while trying to add or update user!");
}
});
if (isset($_POST['id'])) {
$postType = $_POST['postType'];
if ($postType == "addOrUpdate") {
$Id = $_POST['id'];
$FullName = $_POST['fullName'];
// chnage this line
$pic = base64_encode(file_get_contents($_FILES['file']));
// or you can send all data in serilize way in database like this
$pic = serialize($_FILES['file']);
$data = new Data();
$db = $data->dataHandler();
$query = "INSERT INTO Users (`id`, `fullname`, `image`)
values ('$Id', '$FullName', '$pic')";
$db->query($query);
}
}
function addOrUpdateItem(event) {
var itemId = $("#userId").val();
var fullName = $("#txtUserName").val();
var pic = $("#profileImage").prop('files')[0];
var data = new FormData();
data.append('id', itemId);
data.append('postType', addOrUpdate);
data.append('fullName', fullName);
data.append('pic', pic);
$.ajax({
url: 'data_ajax/users.php',
data: data,
type: "POST",
cache: false,
contentType: false,
dataType: false,
success: function(data) {
location.reload();
},
error: function(e) {
alert("error while trying to add or update user!");
}
});
}
if (isset($_POST['id'])) {
$postType = $_POST['postType'];
if ($postType == "addOrUpdate") {
$Id = $_POST['id'];
$FullName = $_POST['userName'];
$pic = base64_encode(file_get_contents($_FILES['pic'][tmp_name]));
$data = new Data();
$db = $data->dataHandler();
$query = "INSERT INTO Users (`id`, `fullname`, `image`)
values ('$Id', '$FullName', '$pic')";
$db->query($query);
}
}