使用 ASP.Net MVC3 显示包含在 byte[] 中的图像
我的观点很强烈。这个强类型有一个由byte[]组成的字段,这个数组包含一张图片。
是否可以使用 @Html.Image(Model.myImage) 之类的内容显示此图像?
非常感谢
I've a view with a strong type. This strong type has a field consisting of a byte[], this array contains a picture.
Is it possible to display this image with something like @Html.Image(Model.myImage) ?
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以创建一个控制器操作方法,该方法将图像作为 FileContentResult 返回:
然后您可以使用模型中的图像 ID 在视图中创建指向操作方法的 ActionLink。
You can create a controller action method that returns an image as a FileContentResult:
Then you can create an ActionLink to the action method in the view using the image id from the model.
这取决于图像有多大。如果它很小,您可以编写一些内容对其进行 base-64 编码并将其嵌入 html 中,喜欢其中任何一个。
对于具体示例< /a>:
如果图像具有任何明显的大小,您可能需要编写一条允许通过图像的某个键进行查找的路由,即类似
/images/{id}
的路由 - 在您获取图像二进制文件并使用return File(bytes, contentType) 的路线
,另外设置缓存标头(并记住重新检查任何必要的安全性)。在你的 html 中你只会有一个(使用 razor 语法,但与 aspx 类似)。
单独的路由方法需要到服务器的额外跃点,但允许在客户端进行缓存(内联 base-64 方法将数据放在每个请求上)。
It depends on how big the image is. If it is small, you could write something to base-64 encode it and embed it in the html, like any of these.
For a concrete example from here:
If the image is of any appreciable size, you may want instead to write a route that allows lookup via some key to the image, i.e. a route like
/images/{id}
- in that route you fetch the image binary and usereturn File(bytes, contentType)
, additionally setting caching headers (and remember to re-check any necessary security). In your html you would just have an(using razor syntax, but similar for aspx).
The separate route approach requires an additional hop to the server, but allows caching at the client (the inline base-64 approach puts the data on every request).
如果您已经将图像作为
byte[]
数组加载到模型中,您可以按照 @Marc Gravell 在 他的回答:这极大地简化了整个过程,您不需要有特定的
FileContentResult
操作方法并再次访问数据库(请参阅@Dmitry S 的answer) 只是为了获取带有图像/照片的byte[]
数组,因为您已经有了它加载到您的模型中。If you already happen to have the image loaded in your model as a
byte[]
array you can do this as @Marc Gravell mentions in his answer:This greatly simplifies the whole process and you won't need to have a specific
FileContentResult
action method and hit the database again (see @Dmitry S's answer) just to fetch thatbyte[]
array with your image/photo since you already have it loaded in your model.听起来您需要一个新操作来获取字节数组(从数据库?)并通过 File 方法....
然后生成一个指向该action的锚点,这样可以在页面加载的同时加载图片,加快显示速度。
Sounds like you'd need a new action that gets the byte array (from a database?) and returns the image via the File method....
Then generate an anchor that points to that action, that way the image can be loaded while the page is loading, speeding up the display.
看一下WebImage类。
http://msdn。 microsoft.com/en-us/library/system.web.helpers.webimage(v=vs.99).aspx
Look at WebImage class.
http://msdn.microsoft.com/en-us/library/system.web.helpers.webimage(v=vs.99).aspx
我会设计一个简单的通用处理程序来为您的图像提供服务。在给定一些参数的情况下,该处理程序可以从数据库加载图像,并将它们写入 http 输出流。
I would design a simple generic handler for serving your images. This handler could, given some parameter, load images from the database, and write them to the http output stream.