C#上传图片代码|.NET快速开发平台|.NET智能表单|.NET工作流

//上传图片

<%@Page language="C#" %>
<%@import namespace="System.IO"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
public void Button_Submit(Object o, EventArgs e)
{
        HttpPostedFile upFile = up_file.PostedFile;
        int iFileLength = upFile.ContentLength;
        try
        {
                if(iFileLength == 0)
                {
                        txtMess.Text = "请选择要上传的文件!";
                }
                else
                {
                        Byte[] FileByteArray = new Byte[iFileLength];
                        Stream StreamObject = upFile.InputStream;
                        StreamObject.Read(FileByteArray, 0, iFileLength);
                        SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
                        string sql = "insert into t_imgs (imgData, type, description, imgSize) values "
                                + "(@Image, @ContentType, @ImageDescription, @ImgSize)";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
                        cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
                        cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
                        cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        txtDesc.Text = "";
                        txtMess.Text = "OK!你已经成功上传了类型的文件";
                }
        }
        catch(Exception ex)
        {
                txtMess.Text = ex.Message.ToString();
        }
}

                               
                       
</script>

<html>
<head>
        <title>上传图片</title>
</head>
<body bgcolor="#FFFFFA">
<form enctype="multipart/form-data" runat="server" id="form1">
<table runat="server" width=700 align=left id="table1" cellpadding=0 cellspacing =0 border=0>
        <tr>
                <td>上传图片</td>
                <td>
                <input type="file" id="up_file" runat="server" style="width:320" accept="text/*" name="up_file">
                </td>
        </tr>
        <tr>
                <td>文件说明</td>
                <td>
                <asp:TextBox runat="server" width=230 id="txtDesc" maintanstate="false" />
                </td>
        </tr>
        <tr>
                <td>
                <asp:label runat="server" id="txtMess" forecolor=red maintainstate="false" />
                </td>
                <td>
                <asp:Button runat="server" width=230  text="上传" />
                </td>
        </tr>
</table>
</form>
</body>
</html>

               
//显示图片

<%@Page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
public void Page_Load(Object o, EventArgs e)
{
        int ImgID = Convert.ToInt32(Request.Params["id"]);
        string connStr = ConfigurationSettings.AppSettings["ConnectionString"];
        SqlConnection conn = new SqlConnection(connStr);
        string sql = "select * from t_imgs where id = @ImgID";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@ImgID", SqlDbType.Int).Value = ImgID;
        conn.Open();
        SqlDataReader read = cmd.ExecuteReader();
        read.Read();
        Response.ContentType = (string)read["type"];
        Response.OutputStream.Write((byte[])read["imgData"], 0, (int)read["imgSize"]);
        Response.End();
        conn.Close();
}
</script>
方正飞鸿智能信息平台 www.founderfix.com
posted @ 2011-03-11 11:39  笨笨的笨笨  阅读(601)  评论(0)    收藏  举报