【问题标题】:How to update image after row is updated in jqgrid在jqgrid中更新行后如何更新图像
【发布时间】:2011-10-02 21:36:49
【问题描述】:

How to show image in jqgrid in edit mode 的代码用于在 jqgrid 中显示图像。

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

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

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

更新

我按照 Oleg 的建议添加了 outputcache 属性。使用提琴手,我验证了来自图像调用的图像响应标头

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 );
    }

respose 头是

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 显示没有检索到当前行图像。

【问题讨论】:

  • 如果您使用 Firebug 或其他 Web 调试器并在保存行后检查网格的 HTML,您是否在 IMG 元素中看到新图像的 URL?
  • @Walter:URL 始终相同。如更新的答案所示,这是包含行 ID 的动态 url。 GetImage 从相同的 url 返回不同的图像。 GetImage 检查行数据并根据行数据返回图像。如果行数据已更改,则行中实际上存在图像列。 Getimage 从数据库中返回该行列值

标签: jqgrid


【解决方案1】:

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

在 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");

更新:我在一个演示项目中使用了HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0));,它生成了<img src=...> 中使用的聊天。 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 标头对我来说似乎很可疑。我没有。

【讨论】:

  • 我认为如果是浏览器缓存问题,HTTP 请求仍然会被调用,只是一旦浏览器读取响应标头并意识到它有图像的缓存副本,传输会结束。
  • @Oleg:非常感谢。我按照您的建议添加了缓存指令,但问题仍然存在。我更新了问题。
  • @Walter Stabosz:我不认为你是对的。一切都取决于站点中使用的默认 HTTP 标头。例如,如果您从上一个答案中打开 the demo 并检查与 FiddlerFirebug 相关的 HTTP 流量,您将看到如果在即。
  • @Andrus:我认为您应该删除 Expires HTTP 标头或将其设置为过去的日期。您可以使用SetExpires。此外,Response.Cache.SetCacheability (HttpCacheability.Public); 行可能应该更好地删除以设置 Cache-Control: private 并禁用代理上的图像缓存(如果存在)。
  • @Andrus:我没有完全符合你的情况,但在我在回答的 UPDATED 部分中描述的非常接近的情况下,我没有你描述的问题。
猜你喜欢
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 2017-07-08
  • 2011-12-18
  • 1970-01-01
相关资源
最近更新 更多