views:

387

answers:

4

k... Strager was able to help me a bit. However, it's still not working quite right. Need someone that knows both MVC and jQuery please...

I have a page that has a image that when clicked on will launch a dialog box that gives the ability to upload a file. Once the dialog closes the image is supposed to refresh with what was uploaded/stored in the database...

All works great the first time. However if I try uploading a 2nd image; the 1st image still displays. It also doesn't seem like my controller method is being called the 2nd time... Below is my code...

I've also eliminated that the page is being cached. Again, the controller method is not being called the 2nd 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>
+1  A: 

What is #image shouldn't it be find('img')? Or give the IMG an ID of 'image'.

Glennular
A: 

Your <img> does not have the id image so .find('#image') isn't returning any elements.

sunsean
+1  A: 

I don't know ASP, but this line:

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

Appears to be parsed at the time the page is initially viewed, not when the new image is uploaded.

To fetch the new image's URL, send that data in the response to the POST request. This should be easy, especially if you use JSON or raw text for the transfer.

For the jQuery side:

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

If anyone knows how to do it on the ASP side, please edit this post or make another!

strager
Can you give me an example of what you were referring to...
Jason
I tried something like this... iphcObject.find('image').attr('src', $.post('<%=Url.Action("GetImage", "Image", new { parentId = Model.ParentId }) %>'));But that didn't work...
Jason
@Jason, See my update.
strager
there is no url to return. it's coming from a database...
Jason
@Jason, Have your request handler return the URL then! You can't do this from the client side, obviously.
strager
The method on the server side is getting the contents from the database and writing it to the response object... Should I be having jquery do the post against a page that outputs the file to the page..?
Jason
@Jason, Yes, that is what should be done. You can change 'json' to 'text' in my code sample if all you output from the request is the image's URL.
strager
I got it to work somewhat. When the dialog closed the image refreshed to the new image. However, if I went right back into the dialog and uploaded a new image the old image was not swapped for the new one... Thoughts..?
Jason
@Jason, Make sure your browser isn't caching your results (returned URL or the image itself).
strager
A: 

I don't know ASP, but... Have you checked the HTTP headers? It could be that the image URL is being cached by the browser. Try LiveHTTPHeaders for Firefox and see what calls are actually making it to the server. Firebug may also be of some assistance here (it tracks cache hits in the browser vs network calls).

martian
definetly not the cache. I have the following in place.... Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore();This prevents the pages from being cached...
Jason
Also, I used LiveHttpHeaders and didn't see any issues there in terms of caching either. The issue truly appears to be when upload and close the dialog box a 2nd time after uploading the first time nothing happens. No request is made...
Jason
If I set a break point on my model method it never fires. Also, nothing happens in LiveHttpHeaders on the 2nd upload...
Jason
If nothing happens in LiveHttpHeaders during the second round, then your browser is likely caching the original image. The cache headers on your pages aren't working. One trick might be to add an extra URL param to the end of the 2nd/3rd image URL, so the browser is forced to refresh the src.
martian
Just to be clear: it sounds like issue is not that your pages are being cached, but that the _images_ are being cached. The Response.Cache.xxxx() methods are likely acting on the pages, rather than the images.
martian
So how can I prevent the image from being cached then..?
Jason
iphcObject.children().find('#image').attr('src', '<%=Url.Action("GetImage", "Image", new { parentId = Model.ParentId }) %>
martian