2012-08-26 167 views
1

我有一个应用程序允许用户通过Web界面将一些文本数据保存到MYSQL数据库中。另外,他们还可以将文件附加到该文本中,并将其保存到blob字段中。附加的文件类型是简单的.txt文件。使用PHP和JavaScript读取blob数据

我能够将这些数据保存到数据库中,但我无法检索它。这是我在做什么,现在找回它:

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

为什么不把附件保存在那个大文件数据库(*你的文件系统*)中,并将它存储在MySQL中可以找到它的路径?你知道,正确的工具等正确的工具等 – eggyal

+0

也应该通过$ _GET ['row']',所以你最终不会得到一个SQL注入。 – devsnd

+0

@twall它在我的模型中被消毒。 – karancan

回答

2

上传部分存在问题。该行

var file = fileInput.files[0]; 

将文件对象指定为file变量。后来,当你将它添加到

"point_of_contact_attachment=" 

它被转换成字符串。所以,你将有

“point_of_contact_attachment = [目标文件]”

,就是这样。

-1

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

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

因为它不是一个文件浏览器可以阅读,它只是下载。

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

+0

仍然只获取[object File] – karancan

+0

尝试擦除 header('Content-type:text /纯); – Julian

+0

等待,这很奇怪,document.location不应该记录任何东西我很确定你仍然使用ajax。 – Julian