create table testTB
(
ID int,
[file] text
)
Upload.aspx
<!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">
<asp:FileUpload ID="file" runat="server" />
<asp:Button ID="btn" runat="server" Text="提交" OnClick="btn_Click" />
<asp:GridView ID="gv" runat="server">
<Columns>
<asp:ImageField DataImageUrlField="id" DataImageUrlFormatString="Show.ashx?id={0}">
</asp:ImageField>
<asp:HyperLinkField HeaderText="连接地址" Text="查看" DataNavigateUrlFields="id" DataNavigateUrlFormatString="Show.aspx?id={0}"
Target="_blank" />
</Columns>
</asp:GridView>
<asp:Repeater ID="rp" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>
ID
</td>
<td>
图片
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("ID") %>
</td>
<td>
<img src='<%#"Show.ashx?id="+Eval("ID") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
Upload.aspx.cs
public partial class UploadFile : System.Web.UI.Page
{
public const string ConnStr = "Data Source=192.168.0.74;Initial Catalog=test;uid=xxx;pwd=xxx";
private void Bind()
{
SqlDataAdapter da = new SqlDataAdapter("select id from testTB", ConnStr);
DataTable dt = new DataTable();
da.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
rp.DataSource = dt;
rp.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void btn_Click(object sender, EventArgs e)
{
if (file.PostedFile != null && file.PostedFile.ContentLength > 0)
{
int size;
Stream fileStream;
size = file.PostedFile.ContentLength;
fileStream = file.PostedFile.InputStream;
Byte[] fileContent = new Byte[size];
fileStream.Read(fileContent, 0, size);
using (SqlConnection con = new SqlConnection(ConnStr))
{
using (SqlCommand cmd = new SqlCommand("insert into testTB values(@ID,@File)", con))
{
cmd.CommandType = CommandType.Text;
SqlParameter id = new SqlParameter("@ID", SqlDbType.Int);
id.Value = DateTime.Now.Millisecond;
cmd.Parameters.Add(id);
SqlParameter filePara = new SqlParameter("@File", SqlDbType.Text);
filePara.Value = Convert.ToBase64String(fileContent);
cmd.Parameters.Add(filePara);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Bind();
Response.Write("success");
}
}
}
}
}
Show.ashx.cs
public class Show : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["id"];
if (!string.IsNullOrEmpty(id))
{
using (SqlConnection con = new SqlConnection(UploadFile.ConnStr))
{
using (SqlCommand cmd = new SqlCommand("select [file] from testTB where id = @ID", con))
{
cmd.CommandType = CommandType.Text;
SqlParameter idPara = new SqlParameter("@ID", SqlDbType.Int);
idPara.Value = id;
cmd.Parameters.Add(idPara);
con.Open();
byte[] pict = Convert.FromBase64String((string)cmd.ExecuteScalar());
con.Close();
context.Response.ContentType = "image/bmp";
context.Response.OutputStream.Write(pict, 0, pict.Length);
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}