from:http://blog.csdn.net/bfcady/article/details/2622701
思路:建立流对象,将上传图片临时保存到byte数组中,再用SQL语句将其保存到数据库中
说明:以下程序一切从简,实际用应用中应注意使用try等语句
- 创建表ImageStore,表结构如下图
- 主页面HTML如下
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>图片上传</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:FileUpload ID="fuImage" runat="server" />
- </div>
- <div>
- <asp:Button ID="btnUpload" runat="server" Text="上传图片" OnClick="btnUpload_Click" />
- </div>
- <div>
- 输入图片的ID:<asp:TextBox ID="txtImageID" runat="server"></asp:TextBox>
- </div>
- <div>
- <asp:Button ID="btnSubmit" runat="server" Text="显示图片"
- onclick="btnSubmit_Click" />
- </div>
- <div>
- <asp:Image ID="Image1" runat="server" />
- </div>
- </form>
- </body>
- </html>
- 主页面后台代码
- using System;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.IO;
- using System.Data.SqlClient;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- int imageLength = fuImage.PostedFile.ContentLength;//取得图片大小,以字节为单位
- byte[] imageByteArray = new byte[imageLength]; //图片将临时存储在Byte(二进制)数组
- Stream imageStream = fuImage.PostedFile.InputStream;//建立流对象
- imageStream.Read(imageByteArray, 0, imageLength);//将图片读取到imageByteArray数组中,其中0为起始位置,imageLength为要读取的长度
- //以下为数据库连接
- SqlConnection sqlconn = new SqlConnection("server=(local);database=a0522183734;uid=a0522183734;pwd=bfcady");
- string sql = "insert imageStore(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@ContentType,@ImageDescription, @ImageSize)";
- SqlCommand sqlcom = new SqlCommand(sql, sqlconn);
- sqlcom.Parameters.AddWithValue("@Image", imageByteArray);
- sqlcom.Parameters.AddWithValue("@ContentType", Path.GetExtension(fuImage.PostedFile.FileName));//取得文件扩展名
- //取得文件名,不包含扩展名,实际应用中作为图片描述用
- sqlcom.Parameters.AddWithValue("@ImageDescription", Path.GetFileNameWithoutExtension(fuImage.PostedFile.FileName));
- sqlcom.Parameters.AddWithValue("@ImageSize", imageLength);
- sqlconn.Open();
- sqlcom.ExecuteNonQuery();
- sqlconn.Close();
- }
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- //改变图片的URL
- Image1.ImageUrl = "ReadImage.ashx?imgID=" + txtImageID.Text;
- }
- }
- 创建一般处理程序ReadImage.ashx,读取图片
-
- <%@ WebHandler Language="C#" Class="ReadImage" %>
- using System;
- using System.Web;
- using System.Data.SqlClient;
- public class ReadImage : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- string imgID = context.Request.QueryString["imgID"].ToString();
- SqlConnection sqlconn = new SqlConnection("server=(local);database=a0522183734;uid=a0522183734;pwd=bfcady");
- string sql = "select * from ImageStore where ImageID=" + imgID;
- SqlCommand sqlcom = new SqlCommand(sql, sqlconn);
- sqlconn.Open();
- SqlDataReader sqldr = sqlcom.ExecuteReader();
- if (sqldr.Read())//移到第一条记录,如果第一条记录不为空
- {
- context.Response.ContentType = (string)sqldr["ImageContentType"];//设置输出类型
- context.Response.OutputStream.Write((byte[])sqldr["ImageData"], 0, (int)sqldr["ImageSize"]);
- context.Response.End();
- }
- sqlconn.Close();
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
- 运行结果如下