from:http://blog.csdn.net/bfcady/article/details/2622701
思路:建立流对象,将上传图片临时保存到byte数组中,再用SQL语句将其保存到数据库中
说明:以下程序一切从简,实际用应用中应注意使用try等语句
- <%@ 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];
-
- Stream imageStream = fuImage.PostedFile.InputStream;
-
- imageStream.Read(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)
-
- {
-
-
-
- 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;
-
- }
-
- }
-
- }
|
请发表评论