dongna2498 2012-10-23 03:40
浏览 116
已采纳

html FileReader:将blob转换为图像文件?

client side code

<head>
<script>
var reader = new FileReader(); 
var objVal;
var image = new Image();
reader.onload = function(e) {
    document.getElementById('propertyImg').setAttribute('src', e.target.result);
};

function readURL(input){    
    if(input.files && input.files[0]){
        reader.readAsDataURL(input.files[0]);
    }
    else {
        document.images[0].src = input.value || "No file selected";
    }
}

function sendPost(){
            var url = 'http://myurl.com';
            var name = document.getElementById('fileInput').files[0].name;
    var data = document.getElementById('propertyImg').getAttribute('src');
    var f = document.createElement("form"); 
    var imgName = document.createElement("input"); 
    var imgData = document.createElement("input"); 
    var f_attr = { 'method' : 'post' , 'action' : url};
    var imgName_attr = {"type" : "hidden", "name" : "img_name", "value" : name};
    var imgData_attr = {"type" : "hidden", "name" : "data", "value" : data};
    setAttributes(f, f_attr);
    setAttributes(imgName, imgName_attr);
    setAttributes(imgData, imgData_attr);
    f.appendChild(imgName);
    f.appendChild(imgData);
    document.body.appendChild(f); 
    f.submit(); 
}

function setAttributes(el, attrs) {
    for(var key in attrs) {
        el.setAttribute(key, attrs[key]);
    }
}
</script>
<body>
   <img id="propertyImg" src="./img/sprite.png"></img>  
   <input type='file' id='fileInput'class='width70_prop' onchange="readURL(this);"></input>
   <button onclick="sendPost()">sendPost</button>
</body>

serverside code

<html>
    <head>
    <?
          $FileName = $_POST['img_name']; 
          $data = $_POST['data']; 
          list($header, $content) = split('[,]', $data); 
          file_put_contents($FileName, base64_decode($content));
          print "Data Written"; 
     ?>
    <script>
    function showImg(){
    var imgSrc =  "<?=$data?>";
    var imgDiv = document.getElementById('imgDiv');
    imgDiv.src = imgSrc;
    }
    </script>
    </head>
    <body>
    <img id='imgDiv'></img>
    <button onclick="showImg()">show</button>
    </body>
</html>

the blob sent to server had infomation about header and its content. when I split header then save decoded its content, it worked.... I changed above code which now works. thx guys

  • 写回答

2条回答 默认 最新

  • donglin1692 2012-10-23 03:50
    关注

    First, there isn't anything in $_POST["data"] because the index (data) is named by the key in the JSON key : value pair, not the JavaScript var.

    Secondly, you should change this:

    $Handle = fopen($FileName, 'w');
    fwrite($Handle, $data); 
    print "Data Written"; 
    fclose($Handle);
    

    to this:

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

报告相同问题?

悬赏问题

  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿