Search code examples
jqueryasp.net-mvctwitter-bootstrapcaching

Remove image cache in modal and update new image on upload


I'm using Asp.net MVC 4 and Bootstrap 3 to upload the image and replace the image with the previous one on modal. So far upload is working but image is not replacing, even if I reload the page and open the modal it still shows the old image which is on cache probably.

Here are my codes.

View that opens 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>

View of the Modal

<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>

How can I remove the image from cache and update the uploaded image in the modal?


Solution

  • Add a query string to your image source like this. This will prevent browser to cache

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