开发者

How to update image after row is updated in jqgrid

Code from How to show image in jqgrid in edit mode is used to show images in jqgrid.

If row is updated (edit action is controller is called), row image is changed in server: image url remains same but new image is returned from server after row save.

jqgrid still shows old image: it does not request new image from server. Pressing grid refresh button also does not request new image. Pressing browser refresh button retrieves new image but this is very inconvenient.

How to show new image after row is updated in jqgrid ?

Update

I added outputcache attribute as Oleg recommends. Using fiddler I verifed that image response header from image call

GET http://localhost:50076/erp/Grid/GetImage?_entity=Artpilt&size=54&id=734 HTTP/1.1
Accept: image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5
Referer: http://localhost:50076/erp/Grid?_entity=Artpilt
Accept-Language: et-EE
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: localhost:50076
If-Modified-Since: Mon, 03 Oct 2011 11:25:29 GMT
If-None-Match: "ArtPilt734"
Connection: Keep-Alive
Cookie: .MyAuth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

is:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 03 Oct 2011 11:17:46 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0
Cache-Control: public, max-age=0, s-maxage=0
Expires: Mon, 03 Oct 2011 11:17:46 GMT
Last-Modified: Mon, 03 Oct 2011 11:17:46 GMT
ETag: "ArtPilt734"
Content-Type: image/jpeg
Content-Length: 1444
Connection: Close

If data in edit form is changed and saved, old image still remains. fiddler shows that tmage is not retrieved from server.

If edit form is closed, old image is shown in grid. Pressing jqgrid refresh button in jqgrid toolbar causes old image still to be displayed. Fiddler shows that new image request is not is not read from server. Only pressing F5 in browser retrieves new image.

How to refresh image immediately if row data is changed in edit form ?

Update2 I think Oleg means HttpCacheability.NoCache , not HttpCacheability.Private as he wrote in comment.

I changed MVC2 controller to

    [OutputCache(Duration = 0, Va开发者_如何学JAVAryByParam = "")]
    public FileContentResult GetImage(string _entity, int id, int? size)
    {
        HttpContext.Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetMaxAge(new TimeSpan(0));

        ... retrieving image and fileextension form database skipped ...
        HttpContext.Response.Cache.SetETag("\"ArtPilt" + id.ToString() + "\"");
        return File(image, "image/" + imagetype );
    }

respose header is

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 03 Oct 2011 13:10:35 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: image/jpeg
Content-Length: 1457
Connection: Close

but problem persists. Fiddler shows that current row image is not retrieved.


It sounds like caching problem. You should set Cache-Control: max-age=0 in HTTP header of the GetImage action. You can consider to set ETag additionally. See this answer for more information.

In ASP.NET MVC program you can use OutputCache attribute

[OutputCache (Duration = 0, VaryByParam = "")]

or

Response.Cache.SetCacheability (HttpCacheability.Public);
Response.Cache.SetMaxAge (new TimeSpan (0));

or something like

Response.AddHeader ("Cache-Control", "max-age=0");

UPDATED: I used HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0)); in one demo project which generate Chat used in the <img src=...>. The HTTP header will be

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 03 Oct 2011 11:59:40 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private, max-age=0
Content-Type: image/png
Content-Length: 55420
Connection: Close

and the corresponding action which generate and provide image will be called every time. The Expires header from the HTTP response which you posted looks suspected for me. I don't have it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜