强制浏览器忽略缓存

5
我的代码裁剪了一张照片。裁剪后的照片不断更新,但浏览器只加载第一个裁剪版本。我在网上搜索过很多方法,但都没有成功。我已经添加了随机字符串到PHP代码中 - new.jpg?time=t,但这会阻止裁剪后的图片保存。
<head>   
    <meta charset="utf-8">   
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="-1" />
</head>

但是浏览器仍从缓存中加载。接下来是HTML、JavaScript和PHP。是否还有其他建议?
                <html>
                <head>
                    <title>Image Crop</title>
                    <style>
                        body{
                            margin: 0;
                            padding: 0;
                        }
                        #container{
                            width: 300px;
                        }
                        #box{
                            position: absolute;
                            top: 0px;
                            left: 0px;
                            width: 100px;
                            height: 100px;
                            background: white;
                            border: 2px solid blue;
                            opacity: 0.5;
                        }
                        #crop_button{
                            background: #333;
                            color: #fff;
                            padding: 5px;
                            border: 0px;
                            margin: 5px;
                        }
                        #output{
                            position: absolute;
                            top: 0px;
                            left: 300px;
                        }
                    </style>
                </head>
                <body>
                    <div id="container">
                        <img src="resized_IMG0934.jpg"/>
                        <div id="box"></div>
                    </div>
                    <div id="output"><image />
                    </div>
                    <button id="crop_button">Crop</button>
                    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
                    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
                    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
                    <script src="index.js"></script>
                </body>
            </html>

                 $(function() {
                $('#box').draggable({containment: '#container'});
                $('#box').resizable({containment: '#container'});
                $('#crop_button').click(function(){ 
                    var top = $('#box').position().top;
                    var left = $('#box').position().left;
                    var width = $('#box').width();
                    var height = $('#box').height();
                    $.post('crop.php', {top:top, left:left, width:width, height:height}, function(){
                        $('#output').html('<img src="new.jpg"/>');
                    });
                });
             });

                <?php
            $dst_x = 0;
            $dst_y = 0;
            $src_x = $_POST['left'];  //crop Start x
            $src_y = $_POST['top'];  //crop Start y
            $dst_w = $_POST['width']; //Thumb width
            $dst_h = $_POST['height']; //Thumb height
            $src_w = $_POST['width'];  //$src_w + $dst_w
            $src_h = $_POST['height'];  //$src_h + $dst_h

            $dst_image = imagecreatetruecolor($dst_w,$dst_h);
            $src_image = imagecreatefromjpeg("resized_IMG0934.jpg");
            imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
            imagejpeg($dst_image, "new.jpg");

你可以始终使用不同的名称替换new.jpg,并替换src属性。 - Sven Delueg
那么你的意思是创建完全不同的名称?但这是否意味着我必须保持取消链接/删除上一个裁剪,以防止我的图像文件夹填满旧的裁剪? - user2790911
3个回答

2
您有以下选择: - 添加以下元标签,这将强制不缓存
<meta http-equiv="pragma" content="no-cache" /> which you have already as I see
  • 此外,如果您想要加载新版本的CSS文件,请按照以下步骤进行:

< link rel="stylesheet" type="text/css" href="mystyle.css?version={NewVersionOnRequierd}">

  • 对于图片也是同样的操作。
< img src="picture.jpg?123" alt="">

是的,我已经尝试过这些了。如果有任何进展,我会向您汇报的。 - user2790911
2023年,大多数浏览器上这个已经不能用了。尽管很久以前它曾经有效。 - Octo Poulos

0
创建一个隐藏的iframe,并将其src设置为以下图像URL:
function refresh(url){
    var iframe = document.createElement('iframe')
    iframe.src = url
}

1
我只想指出,对于那些几年后来到这里的人,这是一个可怕的解决方案。不要这样做。 - Mat Jones

0

好的,最终我通过在图像元素源代码中添加一个随机字符串来解决了问题,因为我尝试了很多次。似乎我的Firefox安装有些问题,很难得出任何结论。感谢大家的贡献。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接