使用文件流保存图片
页面
————————
<%@ 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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1" EmptyDataText="没有可显示的数据记录。" Height="126px" Width="133px">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="DownloadFile.aspx?id={0}"
DataTextField="name" HeaderText="文件名" />
<asp:BoundField DataField="postfix" HeaderText="文件后缀" SortExpression="postfix" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyTestConnectionString1 %>"
DeleteCommand="DELETE FROM [fileInfo] WHERE [id] = @id" InsertCommand="INSERT INTO [fileInfo] ([name], [postfix]) VALUES (@name, @postfix)"
ProviderName="<%$ ConnectionStrings:MyTestConnectionString1.ProviderName %>"
SelectCommand="SELECT [id], [name], [filedata], [postfix] FROM [fileInfo]" UpdateCommand="UPDATE [fileInfo] SET [name] = @name, [postfix] = @postfix WHERE [id] = @id">
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="postfix" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="postfix" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<br />
<asp:Label ID="Label1" runat="server" Height="16px" Text="文件名称:" Width="83px"></asp:Label>
<asp:TextBox ID="txtFileName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="文件路径:" Width="83px"></asp:Label>
<asp:FileUpload ID="fuFile" runat="server" />
<asp:Button ID="Button1" runat="server" Text="保存" OnClick="Button1_Click" Width="46px" /><br />
</div>
</form>
</body>
</html>
后台代码
————————————
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyTestConnectionString1"].ConnectionString))
{
string sql = "insert into fileInfo([name],filedata,postfix) values(@name,@filedata,@postfix)";
SqlParameter paramName = new SqlParameter("@name", this.txtFileName.Text);
byte[] b = new byte[this.fuFile.PostedFile.ContentLength];
fuFile.PostedFile.InputStream.Read(b, 0, b.Length);
SqlParameter paramFile = new SqlParameter("@filedata", SqlDbType.Image);
paramFile.Value = b;
int sign = this.fuFile.FileName.LastIndexOf('.');
string fix = this.fuFile.FileName.Substring(sign + 1, fuFile.FileName.Length - sign - 1);
SqlParameter paramFix = new SqlParameter("@postfix", fix);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Parameters.Add(paramName);
cmd.Parameters.Add(paramFile);
cmd.Parameters.Add(paramFix);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.txtFileName.Text = String.Empty;
this.GridView1.DataBind();
}
}
}