本文介绍了上传后图片src不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ajaxupload插件上载,并且在ajaxupload的OnComplete事件中使用了此功能;

i am making an upload with Ajaxupload plugin and i am using this function in OnComplete event of ajaxupload;

function degis(){
var a = "<?php echo $id; ?>";
document.getElementById("imga").src = "../artwork/"+a+"/logo.jpg?dummy=371662";
document.getElementById("imga").style.width = "500px";
document.getElementById("imga").style.height = "175px";
}

,但是由于某种原因没有出现新上传的图像.我尝试了?dummy = 371662",但没有成功.

but new uploaded image doesnt appear for a reason. i tried that "?dummy=371662" but didnt work.

我也将其用于ajaxupload的Onsubmit事件

i am also using this for Onsubmit event of ajaxupload

function updeg(){
var a = "uploading.gif";
document.getElementById("imga").style.width = "50px";
document.getElementById("imga").style.height = "50px";
document.getElementById("imga").src = a;

}
</script>

这是此元素的html

this is the html of this element

 <img id="imga" alt="" height="175px" src="../artwork/<?php echo $id; ?>/logo.jpg?dummy=371662" width="500px">

对此有何建议?

推荐答案

基于上面的编辑和评论,我认为您需要这样的东西:

Based on your edits and comments above, I think you need something like this:

function junk() {
    return (new Date()).getTime() + Math.round(Math.random());
}

function degis() {
    var img = document.getElementById("imga");
    if (img) {
        img.src = "../artwork/<?php echo $id; ?>/logo.jpg?nocache=" + junk();
        img.style.width = "500px";
        img.style.height = "175px";
    }
}

您先前尝试绕过缓存的操作无效,因为您的虚拟"值每次都相同.如上所述,通过使用junk()函数,您每次都会获得一个不同的随机值,从而确保无法缓存图像.

Your previous attempt to bypass the cache doesn't work because your "dummy" value is the same each time. By use of a junk() function, as above, you get a different random value each time, ensuring that the image cannot be cached.

这篇关于上传后图片src不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 08:53