如何在jqgrid中更新行后更新图像

发布于 2024-12-07 13:28:01 字数 2635 浏览 0 评论 0原文

代码来自 如何在编辑模式下在 jqgrid 中显示图像 用于在jqgrid中显示图像。

如果更新行(调用控制器的编辑操作),则行图像在服务器中更改:图像 url 保持不变,但在行保存后从服务器返回新图像。

jqgrid 仍然显示旧图像:它不会从服务器请求新图像。按网格刷新按钮也不会请求新图像。按浏览器刷新按钮可以检索新图像,但这非常不方便。

如何在 jqgrid 中更新行后显示新图像?

更新

我按照 Oleg 的建议添加了outputcache 属性。使用 fiddler 我验证了图像调用的图像响应标头

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

是:

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

如果编辑表单中的数据被更改并保存,旧图像仍然保留。 fiddler 显示未从服务器检索 tmage。

如果编辑表单关闭,旧图像将显示在网格中。按 jqgrid 工具栏中的 jqgrid 刷新按钮会导致旧图像仍然显示。 Fiddler 显示新的图像请求不是从服务器读取的。只有在浏览器中按 F5 才能检索新图像。

如果编辑表单中的行数据发生更改,如何立即刷新图像?

更新2 我认为 Oleg 的意思是 HttpCacheability.NoCache ,而不是他在评论中写道的 HttpCacheability.Private 。

我更改了 MVC2 控制器以

    [OutputCache(Duration = 0, VaryByParam = "")]
    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 );
    }

响应标头,

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

但问题仍然存在。 Fiddler 显示未检索到当前行图像。

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, VaryByParam = "")]
    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2024-12-14 13:28:01

听起来像是缓存问题。您应该设置 Cache-Control: max-age=0 在 GetImage 操作的 HTTP 标头中。您可以考虑另外设置ETag。有关详细信息,请参阅此答案

在 ASP.NET MVC 程序中,您可以使用 OutputCache 属性

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

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

类似

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

UPDATED 的属性:我使用了 HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0 )); 在一个演示项目中,该项目生成 中使用的聊天。 HTTP 标头

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

每次都会调用 以及生成和提供图像的相应操作。您发布的 HTTP 响应中的 Expires 标头对我来说似乎很可疑。我没有。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文