Default.aspx

View Code
<%@ 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 id="Head1" runat="server">
    <title>将文件添加到数据库中</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <div style="text-align: center">
                <table style="background-image: url(image/background.gif); width: 644px; height: 402px">
                    <tr>
                        <td colspan="11" style="height: 32px">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="6" rowspan="5">
                        </td>
                        <td colspan="2" rowspan="5">
                        </td>
                        <td style="width: 63px; height: 48px">
                            <asp:Label ID="LblChoose" runat="server" Font-Size="Smaller" Text="选择文件:" Width="79px"></asp:Label></td>
                        <td style="width: 123px; height: 48px">
                            <asp:FileUpload ID="FileUpload1" runat="server" /></td>
                        <td style="width: 100px; height: 48px">
                            <asp:Label ID="LblMessage" runat="server" Font-Bold="True" Font-Size="Smaller" ForeColor="Red"
                                Width="102px"></asp:Label></td>
                    </tr>
                    <tr>
                        <td style="width: 63px; height: 14px">
                        </td>
                        <td align="center" style="width: 123px; height: 14px" valign="top">
                            <asp:Button ID="BtnOK" runat="server" OnClick="BtnOK_Click" Text="保存" Width="51px" /></td>
                        <td style="width: 100px; height: 14px">
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 63px; height: 14px">
                        </td>
                        <td style="width: 123px; height: 14px" valign="top">
                        </td>
                        <td style="width: 100px; height: 14px">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" rowspan="2">
                        </td>
                    </tr>
                    <tr>
                    </tr>
                </table>
            </div>
        </div>
    
    </div>
    </form>
</body>
</html>

Default.aspx.cs

View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;

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 BtnOK_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.FileUpload1.PostedFile.FileName != "")
            {
                string ImgPath = FileUpload1.PostedFile.FileName;//获取上传文件的完整路径 
                string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);//获取其上传文件的名称
                string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);//获取文件的后缀名
                int FileLen = this.FileUpload1.PostedFile.ContentLength;//获取上传文件的长度
                Byte[] FileData = new Byte[FileLen];//定义Byte数组
                HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象
                Stream sr = hp.InputStream;//创建数据流对象
                //将图片数据放到image数据对象实例中,其中0代表数组指针起始位置,FileLen表示要读取流的长度
                sr.Read(FileData, 0, FileLen);
                SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
                con.Open();
                SqlCommand com = new SqlCommand("INSERT INTO tb_fileUp (name) VALUES (@imgdata)", con);
                com.Parameters.Add("@imgdata", SqlDbType.Image);//添加参数,并指定Image类型
                com.Parameters["@imgdata"].Value = FileData;//设定参数值
                com.ExecuteNonQuery();//执行添加操作
                this.LblMessage.Text = "保存成功!";
            }
            else
            {
                this.LblMessage.Text = "请选择文件!";
            }
        }
        catch (Exception error)
        {
            this.LblMessage.Text = "处理失败!原因为:" + error.ToString();
        }
    }
}

 

posted on 2013-03-11 02:05  松竹梅  阅读(562)  评论(0编辑  收藏  举报