0
我遇到了将图像保存到本地文件的问题。其他所有的东西看起来都很好,但图像不会只是保存。这是我的代码片段。将RGraph图表保存到本地机器
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);
}
这里是在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);
}
?>
当您运行实际发生的你码?有没有错误?错误是什么意思? – Kenster
我实际上使用它来生成报告。它不会给出任何错误。报告显示没有图表。它不会保存在指定的目录中。 –
如果您使用jQuery来帮助您的AJAX,这意味着您必须编写更少的无关代码。例如$ .post('ajxstuff.php',function(){/ *回调函数* /}) – Richard