【发布时间】:2015-08-06 11:46:51
【问题描述】:
我有下面的代码可以动态显示上传的图像,它工作正常。
$(function () {
$("#fileupload").change(function () {
if (typeof (FileReader) != "undefined") {
var preview = $("#preview");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
$($(this)[0].files).each(function () {
var file = $(this);
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
var img = $("<img />");
img.attr("style", "height: 180px; width: 180px; margin-left:10px; margin-top: 40px;");
img.attr("src", e.target.result);
img.attr("id", "imgUserDisplay");
preview.append(img);
$('#saveImage').on({
'click': function () {
var photodisplay = $("#photodisplay");
var photoimg = $(' <img />');
photoimg.attr("style", "height: 180px; width: 180px; margin-top: 40px;cursor:pointer;");
photoimg.attr("id", "photoUserDisplay");
photoimg.attr("class", "margin");
photoimg.attr("data-target", "#commentDiv");
photoimg.attr("data-toggle", "modal");
photoimg.attr("src", e.target.result);
if (!(photoimg.attr("src") == null || photoimg.attr("src") == '')) {
$('#OnlyPost').attr("style", "display:none;");
$('#ImagePost').removeAttr("style");
$('#imgModalDisplay').attr("src", e.target.result);
photodisplay.append(photoimg);
$('#fileupload').val('');
$("#textareaimg").val('');
preview.html("");
} else {
alert("Un-handeled Exception Caught");
}
}
});
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
preview.html("");
return false;
}
});
} else {
alert("This browser does not support HTML5 FileReader.");
}
});
});
HTML
使用这个元标记来控制缓存,但仍然没有想要的结果
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="0" />
在此代码中,上传的图像显示在不同的 div 中,效果很好。问题是该函数在所有 div 中显示用户之前上传的图像(缓存问题?),这些 div 附加在同一数组中。每次调用此特定函数时,有什么方法可以清除数组(缓存?)?我需要帮助来理解语法。如有必要,可以提供 HTML。
【问题讨论】:
-
使用元标签防止缓存
-
尝试添加一些带有图像名称的参数,例如 test.jpg?time() 这将从服务器而不是从缓存中加载图像
-
有人能建议我如何清除上面 JQuery 函数中的那个数组吗?在它几乎附加到所有 div 之后?这将很有帮助:)
-
使用 manifest.appcache 但仍然显示以前上传的图像,我认为 jquery 函数需要更改。