如何在jqgrid中更新行后更新图像
代码来自 如何在编辑模式下在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来像是缓存问题。您应该设置 Cache-Control: max-age=0 在
GetImage
操作的 HTTP 标头中。您可以考虑另外设置ETag
。有关详细信息,请参阅此答案。在 ASP.NET MVC 程序中,您可以使用
OutputCache
属性或
类似
UPDATED 的属性:我使用了
HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0 ));
在一个演示项目中,该项目生成中使用的聊天。 HTTP 标头
每次都会调用 以及生成和提供图像的相应操作。您发布的 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 setETag
additionally. See this answer for more information.In ASP.NET MVC program you can use
OutputCache
attributeor
or something like
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 beand 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.