Displaying image from database... (copy from ASP.NET Forums)
2006-02-13 13:41
513 查看
Running on ASP.NET 2.0 and SQL Server 2005. Have a table with an image-type column to store a photo. I have already created the script to upload/store the file in the DB, but I need code to handle the output.
I already have a stored proc call the retrieves the data based on an id... the recordset retrieved is one row with the photo and a few other text fields.
Please advise the necessary code to place in the codebehind file to display this photo. I am using <asp:label> tag for the positioning of the image.
Thanks!
-------------------------------------------
Re: Displaying image from database...
I already have a stored proc call the retrieves the data based on an id... the recordset retrieved is one row with the photo and a few other text fields.
Please advise the necessary code to place in the codebehind file to display this photo. I am using <asp:label> tag for the positioning of the image.
Thanks!
-------------------------------------------
Re: Displaying image from database...
Excellent... however, I am not storing the content type or file size in the DB... just the image file. I have this code below that is sending me the file, but there is no image... although the size of the file is the same as the original one uploaded. Any ideas?
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/mysite");
connString = rootWebConfig.ConnectionStrings.ConnectionStrings["strDataSource"];
SqlConnection objConnection = new SqlConnection(connString.ConnectionString);
SqlDataAdapter objCommand = new SqlDataAdapter("SelectProfile", objConnection);
DataSet objDataSet = new DataSet();
objConnection.Open();
objCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
objCommand.SelectCommand.Parameters.Add(new SqlParameter("@user_id", SqlDbType.Char, 50));
objCommand.SelectCommand.Parameters["@user_id"].Value = Request.Params["id"];
objCommand.Fill(objDataSet, "UserAccount");
byte[] fileData = (byte[])objDataSet.Tables["UserAccount"].Rows[0]["photo"];
int fileSize = fileData.Length;
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = false;
Response.ContentType = "image/JPEG";
Response.AddHeader("Content-Disposition", "attachment; filename=MyPhoto.jpg");
Response.AddHeader("Content-Length", fileSize.ToString());
Response.BinaryWrite(fileData);
objConnection.Close();
Response.End();
Re: Displaying image from database...
here are some code bits I have used in the past to get binary data from sql server, but I only know how to response write the data as the full output for a page and then reference it in the html, I would try a searching on google for actually binding binary image data to the page (not sure if it can be done), I seroiusly doubt that you will be able to bind to an asp:label
here is how I have done this in the past
html in an aspx page where you want to show an image from the database
<img src="GetImage.aspx?id=39" />
then you create a GetImage.aspx page that responses gif or jpg data
// get data from db
fileTitle = rs["FileTitle"].ToString();
fileSize = rs["FileSize"].ToString();
contentType = rs["ContentType"].ToString();
byte[] fileData = (byte[]) rs["FileData"];
// response output
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = false;
Response.ContentType = contentType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileTitle);
Response.AddHeader("Content-Length", fileSize.ToString());
Response.BinaryWrite(fileData);
------------------------------------
相关文章推荐
- Displaying image from database... (copy from ASP.NET Forums)
- 电子书下载:Beginning ASP.NET 2.0 Databases From Novice to Professional
- ASP.NET datagrid直接导出为Excel Export to Excel from DataGrid (Using an Microsoft Access database)
- jQuery UI Accordion in ASP.NET MVC - feed with data from database
- Most Popular Questions and Answers on ASP.NET Whidbey(from asp.net forums,write by ASP.NET Team)
- DotNetNuke Announcements(from asp.net forums)
- [收藏]The Baker's Dozen: 13 Tips for Building Database Web Applications Using ASP.NET 3.5, LINQ, and SQL Server 2005 Reporting Services
- asp.net 程序copy SSH文件
- 这两天忙活着 建立一个 dnn Rainbow asp.net forums 整合 和 技术支持的网站
- Displaying XML Files with ASP.NET 2.0
- [Database]创建存储过程并在ASP.NET中的调用带参数的存储过程与及调用带参数的SQL语句对比
- 解决ASP.NET执行update-database命令,无法访问*.mdf本地localDb数据库文件的问题
- Error from MSDN——No3. add subelement in membership Element (ASP.NET Settings Schema)
- ASP.NET中动态获取数据使用Highcharts图表控件【Copy By Internet】
- ASP.net对SQL server数据库里取image类型怎么取
- 支持ASP.NET MVC、WebFroM的表单验证框架ValidationSuar使用介绍
- Using ASP.NET 3.5's ListView and DataPager Controls: Displaying Data with the ListView
- 在Asp.Net Forums新增加了一个中文版块
- Fixing ASP.NET Server Control Rendering Issues with Tag Mapping Image 多出style="border-width:0px"
- 【Asp.Net WebFrom】分页