doufan1363
2012-08-26 23:11 阅读 254
已采纳

使用PHP和JavaScript读取blob数据

I have an application where a user is allowed to save some text data into a MYSQL database through a web interface. In addition, they can also attach a file to this text and I save this into a blob field. The file types that get attached are simple .txt files.

I am able to save this data into the database but I am having trouble retrieving it. This is what I am doing to retrieve it right now:

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

The AJAX request above leads to the following PHP code:

$attachments = $poc -> getPOC($_GET['row']);
header('Content-type: text/plain');
echo $attachments;

The problem I face is that when I console log the data received from the AJAX request I get this:

enter image description here

How do I go about getting the data in simple text format?

Could it be that the way I am uploading the file to the database is incorrect? This is how the file is uploaded to the DB:

    //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条回答 默认 最新

  • 已采纳
    dongtun1683 dongtun1683 2012-08-27 00:31

    There is problem in your upload section. The line

    var file = fileInput.files[0];
    

    assignes file object into file variable. Later, when you add it to

    "point_of_contact_attachment="
    

    it gets converted to string. So you will have

    "point_of_contact_attachment=[object file]"

    And that is it.

    点赞 评论 复制链接分享
  • douyingbei1458 douyingbei1458 2012-08-26 23:32

    try pointing the browser directly to the file instead of using ajax. Like this

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

    Since it is not a file the browser can read, it will just download it.

    However you will still need to get the TXT via ajax, because document.location redirect to the user to a plain text page.

    点赞 评论 复制链接分享

相关推荐