duanlian1978 2017-05-23 16:32
浏览 161

输入文件从js到mysql blob和php输出

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;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥15 用visual studi code完成html页面
    • ¥15 聚类分析或者python进行数据分析
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备
    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复