0

我有一个 HTML5 应用程序,它将画布图像保存到服务器,然后提供指向该图像的链接,该图像在新窗口中打开。

这在我第一次保存时效果很好,但是如果我创建并保存一个新图像,然后单击链接,它会显示之前创建的旧图像。

单击刷新将强制它显示新的,但我想知道是否有办法确保它显示正确的图像,这样我就不必刷新页面?

下面是我用来保存图像的内容。

<script>
function saveImageAs (imgOrURL) {
if (typeof imgOrURL == 'object')
  imgOrURL = imgOrURL.src;
window.win = open (imgOrURL);
setTimeout('win.document.execCommand("SaveAs")', 500);
}
</script>

<script type="text/javascript">

//****************************************************************
// Save canvas content into image file.                         //
//****************************************************************
function saveViaAJAX()
{
document.getElementById('saveimage').style.visibility="hidden";
document.getElementById("debugFilenameConsole").innerHTML="Please wait while your image is been generated";

var testCanvas = document.getElementById('canvas');
var canvasData = testCanvas.toDataURL("image/jpg");
var postData = "canvasData="+canvasData;
var debugConsole= document.getElementById("debugConsole");
debugConsole.value=canvasData;

//alert("canvasData ="+canvasData );
var ajax = new XMLHttpRequest();
ajax.open("POST",'savecanvas.php',true);
ajax.setRequestHeader('Content-Type', 'canvas/upload');
//ajax.setRequestHeader('Content-TypeLength', postData.length);

ajax.onreadystatechange=function()
{
    if (ajax.readyState == 4)
    {
        //alert(ajax.responseText);
        // Write out the filename.
            document.getElementById("debugFilenameConsole").innerHTML="Saved as <a target='_blank' href='myimage.php'> MyImage.jpg"+ajax.responseText+"</a><br>Reload this page to start a new image or click on the link above to open the file.";
    }
}
ajax.send(postData);
    }
    </script>

和 PHP

    <?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( 'MyImage.jpg', 'wb' );
    fwrite( $fp, $unencodedData);
    fclose( $fp );
    }
    header("Content-Type: image/jpg");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("content-disposition: attachment; filename=MyImage.jpg");
    imagejpeg($img, null, 100);
    ?>
4

2 回答 2

2

因为您为图像提供了相同的名称和路径,所以浏览器会为您缓存它。

使用独特的路径来避免这种情况!

于 2012-11-08T13:26:45.570 回答
1

你可以试试

var random=new Date();
var random2=getTime();
var rand=random+random2;

imgOrURL = imgOrURL.src+'?rnd='+rand

或者对于 PHP

$rand=rand();
MyImage.php?rand=<?=$rand?>

或者

$image='MyImage.php?rand='.$rand.'';

以备日后使用,免头痛。首先添加日期和时间,因为使用md5sha1()rand()JavaScript 唯一键可能是一场噩梦,例如:

/// This is much cleaner

/11.1.2012/11.05/eachier93.jpg
/11.1.2012/11.05/4358390485/93.jpg
/11.1.2012/11.10/3249203489834/234234.jpg

/// then this

/files/342748234234234/234982348394/333535.jpg
/files/4535345345/234234234234/3332.jpg
/files/23423434324/023840348234/2343.jpg
于 2012-11-08T13:32:22.563 回答