在模态中删除图像缓存并在上载时更新新图像

时间:2016-01-13 11:56:58

标签: jquery asp.net-mvc twitter-bootstrap caching

我使用asp.net mvc 4Bootstrap 3上传图片,并将图片替换为modal上的图片。到目前为止上传工作但图像没有替换,即使我重新加载页面并打开模式它仍然显示可能在缓存上的旧图像。这是我的代码,

打开模态的视图

<script>
    $('.btnInfo').click(function (e) {
        var id = $(this).attr('href');
        e.preventDefault();
        $('#modal-content').load("FlatImageOne?FlId=" + id);
    });
</script>

<a href="@Model.serial" class="btnInfo" data-target="#myModal" data-toggle="modal">Change Featured Image</a>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Change Image</h4>
            </div>
            <div class="modal-body">
                <div id="modal-content"></div>
            </div>
        </div>
    </div>
</div>

模态视图

<script>
    $(document).ready(function () {
        $('#btnUpload').click(function (e) {
            e.preventDefault();
            var getFile = document.getElementById('file').files[0];
            var getId = $("#getFlId").val();
            var formdata = new FormData();
            formdata.append("file", getFile);
            formdata.append("getFlId", getId);
            $.ajax({
                url: '@Url.Action("FlatImageOne", "Home")',
                type: 'POST',
                data: formdata,
                processData: false,
                contentType: false,
                success: function (data) {
                    alert("Upload successful!");
                    $('#imgOne').empty();
                    $('#imgOne').html('<img src="/Images/Flats/' + getId + '-0.png" alt="your image" class="img-responsive" />');
                },
                error: function () {
                    alert("Error! Please Try Again!");
                }
            });
        });
    });
</script>

<div>
    @using (Html.BeginForm("FlatImageOne", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary(true, "Error! Please provide valid information!")

        <input type="file" name="file" id="file" style="width: 100%;" /><br />
        <input type="hidden" name="getFlId" id="getFlId" value="@ViewBag.ImgName" />
        <input id="btnUpload" type="submit" class="btn btn-info" value="Upload" />
    }
</div><br />
<div id="imgOne">
    <img src="~/Images/Flats/@(ViewBag.ImgName)-0.png" alt="your image" class="img-responsive" />
</div>

如何从缓存中删除图像并更新模态中的上传图像?急需这个帮助!感谢。

2 个答案:

答案 0 :(得分:2)

像这样为您的图片来源添加 query string 。这将阻止浏览器缓存

$('#imgOne').html('<img src="/Images/Flats/' + getId + '-0.png?' + (new Date()).getTime() + '" alt="your image" class="img-responsive" />');

答案 1 :(得分:2)

替换以下行

$('#imgOne').html('<img src="/Images/Flats/' + getId + '-0.png" alt="your image" class="img-responsive" />');

使用下面一行强制浏览器重新加载图片。

$('#imgOne').html('<img src="/Images/Flats/' + getId + '-0.png?"' + new Date().getTime() +' alt="your image" class="img-responsive" />');