什么是显示在asp.net SQL Server数据库中的图像的最佳方式? [英] What's the best way to display an image from a sql server database in asp.net?

查看:135
本文介绍了什么是显示在asp.net SQL Server数据库中的图像的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有该图像返回字节SQL Server数据库。如果我使用的TableAdapter向导,并将其设置为我的存储过程和preVIEW数据,它拉回来的图像。它会自动把它变成在preVIEW数据的图像。我不认为这是个整数的字符串或任何东西。

我如何与一个GridView和ObjectDataSource显示它在我的asp.net的网页?

我已经搜查,foudn其中的ImageField可以指向一个url另一个页面,做字节改造上,但我不知道它是最好的。我发现创建一个临时文件的另一种方式。

只是想看看做到这一点的最好办法。

编辑 - 我想不使用临时文件。如果我不能使用GridView经常像场就可以了。

asp.net 2.0,C#。

感谢您的任何帮助。

修改

结束:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        字符串ID =请求[ID];
        字符串connstr =DSN = MYSERVER;
        OdbcConnection康恩=新OdbcConnection(connstr);
        的OdbcCommand CMD =新的OdbcCommand({调用mySP()}?,conn);在
        cmd.CommandType = CommandType.StoredProcedure;        //添加输入参数,并设置其属性。
        OdbcParameter参数=新OdbcParameter();
        parameter.ParameterName =@MyParam;
        parameter.OdbcType = OdbcType.VarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = ID;        //参数添加到Parameters集合。
        cmd.Parameters.Add(参数);
        conn.Open();
        OdbcDataReader博士= cmd.ExecuteReader();
        而(dr.Read())
        {
            字节[]缓冲=(字节[])博士[0];
            Response.ContentType =图像/ JPG
            Response.BinaryWrite(缓冲液);
            Response.Flush();
        }
    }

和这个调用页面上:

 < ASP:图片ID =Image1的ImageAlign =中东的ImageUrl =?show.aspx ID = 123=服务器/>


解决方案

有两种选择:

创建一个临时文件 - 这种方法的问题是,你必须创建该文件,这意味着你的网站必须有一个目录这是不是一个伟大的事情写访问。您还需要有一种方法来清理图像。

从其他网址提供了 - 这是我的preferred方法,因为你不需要任何磁盘访问。一个简单的HTTP处理程序(ASHX)是服务了图像的伟大的方法。

修改

如果您需要在ashx的会话状态,请上网:<一href=\"http://stackoverflow.com/questions/293276/asp-net-system-web-httpcontext-current-session-null-in-global-asax\">http://stackoverflow.com/questions/293276/asp-net-system-web-httpcontext-current-session-null-in-global-asax.

修改

夫妇更多的心思。有些情况下,使用临时文件可能会更好。例如,如果你的图像是由很多用户频繁请求。然后存储在磁盘上的图像将是有意义的,因为你可以写文件一次,这确实增加了复杂性 - 维持,但根据交通它可能是值得的,因为这将让你避免回调到.NET堆栈,并充分利用IIS静态内容缓存。

I have a sql server database that returns byte for the image. If I use the tableadapter wizard and set it to my stored procedure and preview data, it pulls back an image. It automatically turns it into an image in the preview data. I don't see it as a string of Ints or anything.

How can I display it on my asp.net webpage with a gridview and objectdatasource?

I have searched and foudn where the imagefield can point to a url on another page that does the byte transformation but I'm not sure it's the best. I found another way that creates a temp file.

Just trying to see the best way to do it.

edit - I am trying not to use a temp file. If I cannot use a gridview a regular image field is ok.

asp.net 2.0, c#.

Thank you for any help.

edit

ended up with:

   protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request["id"];
        string connstr = "DSN=myserver";
        OdbcConnection conn = new OdbcConnection(connstr);
        OdbcCommand cmd = new OdbcCommand("{call mySP (?)}", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        OdbcParameter parameter = new OdbcParameter();
        parameter.ParameterName = "@MyParam";
        parameter.OdbcType = OdbcType.VarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = id;

        // Add the parameter to the Parameters collection. 
        cmd.Parameters.Add(parameter);
        conn.Open();
        OdbcDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            byte[] buffer = (byte[])dr[0];
            Response.ContentType = "image/jpg";
            Response.BinaryWrite(buffer);
            Response.Flush();
        }
    }

and this on the calling page:

<asp:Image ID="Image1" ImageAlign="Middle" ImageUrl="show.aspx?id=123" Runat="server" />

解决方案

Two options:

Create a temp file - The problem with this approach is that you have to create the file, which means your web must have write access to a directory which is not a great thing. You also need to have a way to clean up the images.

Serve it from another URL - This is my preferred method, as you have no disk access required. A simple http handler (ashx) is a great method to serve up the image.

Edit

If you need session state in the ashx, check out: http://stackoverflow.com/questions/293276/asp-net-system-web-httpcontext-current-session-null-in-global-asax.

Edit

Couple more thoughts. There are some cases where using a temp file might be better. For example if your images are requested frequently by a lot of users. Then storing the images on the disk would make sense, since you could write the file once, this does increase the maintance complexity but depending on traffic it might be worth it since this would let you avoid calling back into the .net stack and leverage IIS caching of static content.

这篇关于什么是显示在asp.net SQL Server数据库中的图像的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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