我有一个应用程序,允许用户通过 Web 界面将一些文本数据保存到 MYSQL 数据库中。此外,他们还可以将文件附加到此文本,我将其保存到 blob 字段中。附加的文件类型是简单的 .txt 文件。
我可以将这些数据保存到数据库中,但我无法检索它。这就是我现在正在做的取回它的事情:
//Events that take place when trying to retreive an attached file
function getFile(rowid){
//Make an AJAX Request to fetch the file
$.ajax({
type: 'get',
url: 'point-of-contact.php',
data: 'page=attachment&row='+rowid,
dataType: 'text',
success: function(data) {
console.log (data);
}
});
}
上面的 AJAX 请求导致以下 PHP 代码:
$attachments = $poc -> getPOC($_GET['row']);
header('Content-type: text/plain');
echo $attachments;
我面临的问题是,当我控制台记录从 AJAX 请求收到的数据时,我得到了这个:
如何获取简单文本格式的数据?
会不会是我上传文件到数据库的方式不对?这是文件上传到数据库的方式:
//File upload code
var fileInput = document.getElementById('upload');
var file = fileInput.files[0];
//Hide the save button
$("#save-button-1").hide();
//Make the AJAX request
$.ajax({
type: 'post',
url: 'point-of-contact.php?page=add',
data: 'point_of_contact=' + $("#textarea1").val() + '&point_of_contact_attachment=' + file,
success: function(data) {
$('#done-1').show();
setTimeout(function() {
$('#done-1').fadeOut();
}, 2500);
$('.loader').fadeOut();
}
});
最佳答案
您的上传部分有问题。线路
var file = fileInput.files[0];
将文件对象分配给 file
变量。稍后,当您将其添加到
"point_of_contact_attachment="
它被转换为字符串。所以你会有
“point_of_contact_attachment=[目标文件]”
就是这样。
关于php - 使用 PHP 和 JavaScript 读取 blob 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12134658/