使用PHP和JavaScript读取blob数据

时间:2012-08-26 23:11:59

标签: php javascript mysql blob

我有一个应用程序,允许用户通过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请求收到的数据时,我得到了这个:

enter image description here

如何以简单的文本格式获取数据?

我将文件上传到数据库的方式是不正确的?这是文件上传到数据库的方式:

    //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();
        }
    });

2 个答案:

答案 0 :(得分:2)

您的上传部分存在问题。这条线

var file = fileInput.files[0];

将文件对象分配到file变量中。稍后,当您将其添加到

"point_of_contact_attachment="

它被转换为字符串。所以你会有

“point_of_contact_attachment = [object file]”

就是这样。

答案 1 :(得分:-1)

尝试将浏览器直接指向文件而不是使用ajax。 喜欢这个

document.location = point-of-contact.php?page=attachment&row='+rowid;

由于它不是浏览器可以读取的文件,因此只需下载即可。

但是你仍然需要通过ajax获取TXT,因为document.location会将用户重定向到纯文本页面。

相关问题