dongxiong1935 2015-07-10 15:25 采纳率: 0%
浏览 79

将RGraph图表保存到本地计算机

I am having issues saving images to local file. Every other things look fine but the image won't just save. Here is a snippet of my code.

    function saveImage(){
    var xmlhttp;
    xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            //do something with the response
        }
    }
    xmlhttp.open("POST","ajxstuff.php",true);
    var oldCanvas = document.getElementById('cvs').toDataURL("image/png");
    var img = new Image();
    img.src = oldCanvas;
    xmlhttp.setRequestHeader("Content-type", "application/upload")
    xmlhttp.send(oldCanvas);
}

Here is the the ajxstuff.php

<?php

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
    // Get the data like you would with traditional post
    $rawImage=$GLOBALS['HTTP_RAW_POST_DATA'];

    // Remove the headers  
    $removeHeaders=substr($rawImage, strpos($rawImage, ",")+1);

    // decode it from base 64 and into image data only
    $decode=base64_decode($removeHeaders);

    // save to your server
    $saveName = 'C:\Users\Administrator\Downloads\image009.png';
    $fopen = fopen($saveName, 'wb' );
    fwrite( $fopen, $decode);
    fclose( $fopen );
}

?>
  • 写回答

1条回答 默认 最新

  • douwuli4512 2015-07-11 09:23
    关注

    OK, here's my revised answer: You could instead of sending the image as (what looks like) a file, send it as text - since that's what you get from toDataUrl() (a base64 encoded representation of the image).

    $.post('ajxstuff.php',{
        myChart: document.getElementById('cvs').toDataURL()
    }, function ()
    {
        /* callback */
    })
    

    And in your PHP script the data would show up in $_POST['myChart'] - and you could write it to a file like this:

    <?php
        $data = base64_decode($_POST['myChart']);
        file_put_contents('C:\Users\Administrator\Downloads\image009.png', $data);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错