Refresh Src Image

k ... Strager was able to help me a little. However, it still does not work. Need someone who knows both MVC and jQuery, please ...

I have a page with an image that, when clicked, will launch a dialog box that allows you to upload a file. After closing the dialog box, the image should be updated with what was loaded / saved in the database ...

Everything works great for the first time. However, if I try to upload a second image; 1st image is displayed. It also doesn't look like my controller method is called a second time ... Below is my code ...

I also ruled out that the page is cached. Again, the controller method is not called the second time around ...

controller

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetImage(Guid parentId)
{
    var la = Helper.Service.GetAttachmentByEntity(parentId, MembershipProvider.SecurityTicket).ToList();
    la.OrderByDescending(x => x.CreatedDate);

    return File(la[0].Data, la[0].ContentType);
}

View

<script type="text/javascript">
    $(function() {
        var iphcObject = $('#<%=imagePlaceHolderControl.ClientID %>');

        iphcObject.children().find('#imageUploadDialog').bind('onClose', function() {
            iphcObject.children().find('#image').attr('src', '<%=Url.Action("GetImage", "Image", new { parentId = Model.ParentId }) %>');
        });
    });
</script>

<asp:Panel ID="imagePlaceHolderControl" runat="server">
    <div style="border: solid 1px; width: <%=Model.Width%>; height: <%=Model.Height%>; text-align: center; vertical-align: middle;">
        <a href="#" onclick="$('#imageUploadDialog').dialog('open');" title="Add a picture...">
        <img id="image" src="<%=Url.Content("~/content/images/icon_individual_blank.gif") %>" /></a>
        <%Html.RenderDialog("imageUploadDialog", "Upload image...", "ImagePlaceHolder_Upload", "Image", Model, 235, 335); %>        
    </div>
</asp:Panel>
+3
5

#image, ('img')? IMG "".

+1

ASP, :

iphcObject.find('#image').attr('src', '<%=Url.Action("GetNewImage", "Image", new { parentId = Model.ParentId }) %>');

, .

URL , POST. , JSON .

jQuery:

$.post('<%=Url.Action("GetImage", "Image", new { parentId = Model.ParentId }) %>', {}, function(data) 
{
    iphcObject.find('#image').attr('src', data.newImageUrl);
}, 'json');

- , ASP, !

+1

:

  <img id="img" src="@Url.Action("GetImage")" alt="random" />
  <script type="text/javascript">

     var src = $("#img").attr("src");
     var count = 0;

     setInterval(function () {
        $("#img").attr("src", src + "?" + count);
        count++;
     }, 3000);

  </script>
+1

<img> image, .find('#image') .

0

I do not know ASP, but ... Have you checked the HTTP headers? The image URL may be cached by the browser. Try LiveHTTPHeaders for Firefox and see what calls it actually makes on the server. Firebug can also be useful here (it tracks browser caching and network calls).

0
source

Source: https://habr.com/ru/post/1702602/


All Articles