drtoclr046994545 2012-05-04 14:49
浏览 61
已采纳

将Canvas保存为服务器上的图像时出现setRequestHeader错误

I have a highcharts svg which I have converted to a png via canvg and displayed on the webpage. I would like to then send this png to the server to create a link to the image. I was following the answer code on another topic:

Renaming an image created from an HTML5 canvas

My javascript code is the following:

var timeout = window.setTimeout(function() {
    canvg(document.getElementById('canvas'), chart.getSVG());
}, 10000);

var canvas = document.getElementById("canvas");

var img = canvas.toDataURL("images/png");
document.write('<img src="'+img+'"/>');

saveDataURL(img)

function saveDataURL(canvas) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            window.location.href = request.responseText;
        }
    };
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.open("POST", "saveDataURL.php", true);
    request.send("dataURL=" + canvas.toDataURL());
}

My php called saveDataURL.php is then:

$dataURL = $_POST["dataURL"];
$encodedData = explode(',', $dataURL)[1];
$decodedData = base64_decode($encodedData);
file_put_contents("temp/faizan.png", $decodedData);
echo "http://whichchart.com/temp/faizan.png";

In firefox 12 the following error is thrown up at the "request.setRequestHeader" line:

Component returned failure code: 0x804b000f (NS_ERROR_IN_PROGRESS) [nsIXMLHttpRequest.setRequestHeader] http://whichchart.com/oil.js Line 102

In chrome the error on the same line is:

Uncaught Error: INVALID_STATE_ERR: DOM Exception 11 saveDataURLoil.js:106 (anonymous function)

The example can be viewed here: whichchart.com. Can you help? Thanks.

  • 写回答

1条回答 默认 最新

  • duanchi0897 2012-05-05 00:20
    关注

    Ok, so I found a different solution after much searching. The link is here:

    http://permadi.com/blog/2010/10/html5-saving-canvas-image-data-using-php-and-ajax/

    Assuming you have a canvas called testCanvas, this javascript and php will work:

    var canvasData = testCanvas.toDataURL("image/png");
    var ajax = new XMLHttpRequest();
    ajax.open("POST",'testSave.php',false);
    ajax.setRequestHeader('Content-Type', 'application/upload');
    ajax.send(canvasData );  
    
    <?php
    if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
    {
        // Get the data
        $imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
    
        // Remove the headers (data:,) part. 
        // A real application should use them according to needs such as to check image type
        $filteredData=substr($imageData, strpos($imageData, ",")+1);
    
        // Need to decode before saving since the data we received is already base64 encoded
        $unencodedData=base64_decode($filteredData);
    
        //echo "unencodedData".$unencodedData;
    
        // Save file.  This example uses a hard coded filename for testing,
        // but a real application can specify filename in POST variable
        $fp = fopen( 'test.png', 'wb' );
        fwrite( $fp, $unencodedData);
        fclose( $fp );
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug