MVC在视图中显示文件图标 [英] MVC display a file icon in a view

查看:160
本文介绍了MVC在视图中显示文件图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从本地存储的文件中提取文件图标。



家庭控制器:

I want to extract the file icon from a locally stored file.

Home controller:

public byte[] Icon()
{
    Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(@"C:/Users/Filip/Desktop/test.txt");

    Bitmap iconPng = icon.ToBitmap();

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        iconPng.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }
}



查看:


View:

< img src="@Url.Action("Icon", "Home")" />



为什么这不起作用?我得到的只是一张X的小图片。我做错了什么?



感谢你能给我的所有帮助,谢谢!


Why is this not working? All im getting is a small picture of an X. What am i doing wrong?

Appreciate all the help you can give me, thanks!

推荐答案

根据定义img元素src属性必须有一个指向图像的URL ...

在你的情况下,你推入一些二进制值,但img尝试将其解释为url - X(缺失图像)图片的原因。
有一种非标准但主要支持的将二进制数据推送到img元素的方法,但在这种情况下格式有点不同...

By definition img element src attribute must have a url that points to an image...
In your case you push into it some binary value but img try to interpret it as url - that the reason for the X (missing image) picture.
There is a non-standard, but mostly supported way of pushing binary data into img element, but in that case the format is a bit different...
<img src="data:image/jpeg;base64{binary data in base64}" />



---

我从来没有在MVC中完成它,但在WebForms中有一个样本,它可能会给你方向......


---
I've never done it in MVC, but have a sample in WebForms, that may give you the direction...

//pic.aspx
public<pre> partial class Pic : Page
{
  protected override void OnLoad ( EventArgs e )
  {
    // get the image file from page query parameters
    byte[ ] bBuffer = File.ReadAllBytes( szPath );

    Response.Clear( );

    Response.Buffer = true;

    // send to browser
    Response.OutputStream.Write( bBuffer, 0, bBuffer.Length );

    Response.Flush( );

    Response.End( );</pre>
  }
}




<!-- usage in web page -->
<img atl="me" src="Pic.aspx?path=me.jpg" />



只要没有真实文件就可以完成你可以得到图像的二进制内容,假设使用内存流或某种东西......


It's can be done also without real files as long as you can get the binary content of the image, let say using memory stream or some sort of thing...


这篇关于MVC在视图中显示文件图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆