I'm getting crazy about this: I have an input file, and through js and I get the content. Now, I need to store it in mysql blob, and then I need to use php to output or save the file (which, for knowing, is a pdf or a jpg).
The problem is that I get a blank file or a corrupted one! I've tried all of the FileReader method, without success: readAsDataURL gives me a corrupted, readAsBinaryString an empty... how can I achieve this?
My code so far for JS:
reader.onload = function(evt) {
if(evt.target.readyState != 2) return;
if(evt.target.error) {
alert('Error while reading file');
return;
}
filecontent = evt.target.result;
$.ajax({
url: 'xxx',
type: "POST",
data: {
visura:filecontent,
visura_nome:$("#visura").get(0).files[0].name
},
ajax: false,
scriptCharset: "utf-8",
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
}).done(function (response) {
...
return false;
});
return false;
};
reader.readAsBinaryString($("#visura").get(0).files[0]);
For PHP:
$file=$row["visura"];
$visura_nome=explode(".",$row["visura_nome"]);
$visura_nome=$visura_nome[count($visura_nome)-1];
if($visura_nome=="pdf"){
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=visura.pdf");
header("Content-Disposition:filename=visura.pdf");
}else{
header("Content-type:image/".$visura_nome);
header("Content-
Disposition:attachment;filename=visura.".$visura_nome);
header("Content-Disposition:filename=visura.".$visura_nome);
}
echo $file;