ASP.NET MVC通过MVC框架加载图像缓慢?
在某些相册页面上,我想显示大约 20 个缩略图。这些缩略图是以编程方式从数据库加载的。这些缩略图已经调整大小。当我向他们展示图像时,图像加载有点慢。有些需要 0.5 秒加载,有些需要等待 2 秒。数据库并不重要,因为当我删除数据库层时,性能问题仍然存在。当我直接使用 html 加载相同的图像时,问题会立即加载。
通过 mvc 框架加载图像/文件是否很慢或者我错过了什么?
这太慢了
//in html
<img src='/File/Image.jpg' border='0'>
//in controller
public FileResult File(string ID)
{
//database connection removed, just show a pic
byte[] imageFile = System.IO.File.ReadAllBytes(ID);
return new FileContentResult(imageFile,"image/pjpeg");
}
这立即进行
<img src='/Content/Images/Image.jpg' border='0'>
On some photobook page i want to show appr 20 thumbnails. These thumbnails are programatically loaded from a database. those thumbnails are already resized. When i show them the images load kinda slow. some take 0.5 seconds to load some wait for 2 secons. The database doesn't matter because when i remove the database layer, the performance issue still exists.When i load the same images directly with html the problem the images do load immediately.
Is loading images/files through the mvc framework slow or am i missing something?
This goes too slow
//in html
<img src='/File/Image.jpg' border='0'>
//in controller
public FileResult File(string ID)
{
//database connection removed, just show a pic
byte[] imageFile = System.IO.File.ReadAllBytes(ID);
return new FileContentResult(imageFile,"image/pjpeg");
}
This goes immediately
<img src='/Content/Images/Image.jpg' border='0'>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的问题。我正在使用 MVC 3。在费尽心思之后,我发现一旦你在 Web 应用程序中使用会话状态,由于会话请求的冲击,动态图像加载似乎会被阻塞。为了解决这个问题,我用以下内容装饰了我的控制器:
[SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]
这禁用了我的照片控制器的会话状态,并返回了速度。如果您使用的是早期版本的 MVC,则需要跳过一些步骤并创建一个控制器/控制器工厂来执行此操作。请参阅如何在 ASP.NET MVC 中禁用会话状态?
希望这有帮助!
I had the same issue. I'm using MVC 3. After pulling my hair out, what I discovered is that once you use Session State in your web app, dynamic image loading seems to get clogged, due to the pounding session requests. To fix this, I decorated my controller with:
[SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]
This disabled the session state for my Photos controller, and the speed returned. If you are using an earlier version of MVC, you'll need to jump through some hoops and create a Controller/Controller factory to do this. See How can I disable session state in ASP.NET MVC?
Hope this helps!
您通过 MVC 公开图像来增加处理开销。当您直接链接到图像时,它由 IIS 自动处理,而不是 MVC 管道,因此您可以跳过大量开销。
此外,通过加载到字节数组中,您可以将完整图像从磁盘加载到内存中,然后将其流式传输出去,而不是直接从磁盘流式传输。
这样您可能会获得稍好的性能:
但是它不会像静态文件完全跳过 MVC 那样快。
如果上面的内容为您解决了这个问题,您可能会想修改缓存参数。
You are adding processing overhead by exposing the image via MVC. When you directly link to an image, it is handled automatically by IIS, rather than the MVC pipeline, so you skip a lot of overhead.
Also, by loading into a byte array, you're loading the full image from disk into memory and then streaming it out, rather than just streaming directly from disk.
You might get slightly better performance with this:
But it's not going to be quite as fast as skipping MVC altogether for static files.
If the above fixes it for you, you'll probably want to mess around with the caching parameters.