doufan1363 2012-08-26 23:11
浏览 343
已采纳

使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?