且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是显示在asp.net SQL Server数据库中的图像的***方式?

更新时间:2023-02-16 12:27:00

有两种选择:

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

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

修改

如果您需要在ashx的会话状态,请上网:http://***.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://***.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.