Tks Palpy
--
Sent via http://csharpindex.com
C# Index to resources
Max Chen wrote:
>
> /html; charset=3Dgb2312">
> <META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY>
> <DIV><FONT face=3DArial size=3D2>UploadImage.aspx.cs </FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> public void
> UploadBtn_Click(object sender,
> System.EventArgs e)<BR> {<BR> if
> (Page.IsValid) //save the
> image<BR> {<BR> Stream imgStream
> =3D
> UploadFile.PostedFile.InputStream;<BR> int imgLen
> =3D
> UploadFile.PostedFile.ContentLength;<BR> string
> imgContentType =
> UploadFile.PostedFile.ContentType;<BR> string
> imgName =
> txtImgName.Value;<BR> byte[] imgBinaryData = new
> byte[imgLen];<BR> int n =
> imgStream.Read(imgBinaryData,0,imgLen);<BR> <BR>&n=
> bsp; int
> RowsAffected = SaveToDB( imgName,
> imgBinaryData,imgContentType);<BR> if (
> RowsAffected>0
> )<BR> {<BR> Response.=
> Write("<BR>The
> Image was
> saved");<BR> }<BR> else<BR>=
> {<BR> Response.Write=
> ("<BR>An
> error occurred uploading the
> image");<BR> }</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial
> size=3D2> }<BR> }</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> private void
> InitializeComponent()<BR> {<BR> this.UploadBt=
> n.Click
> +=3D new System.EventHandler(this.UploadBtn_Click);</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> }</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><BR><FONT face=3DArial size=3D2> private int
> SaveToDB(string
> imgName, byte[] imgbin, string
> imgcontenttype)<BR> {<BR> //use the
> web.config to
> store the connection string<BR> SqlConnection
> connection = new
> SqlConnection("server=3Dlocalhost;uid=3DUrsa;pwd=3Dursa;Database=3DUrsaTe=
> st");<BR> //SqlCommand
> command = new SqlCommand( "INSERT INTO TestImage
> (img_name,img_data,img_contenttype) VALUES ( @img_name,
> @img_data,@img_contenttype )", connection
> );<BR> SqlCommand
> command = new SqlCommand( "INSERT INTO TestImage
> (TestImg_Name,TestImg_Data,TestImg_Type) VALUES ( @img_name,
> @img_data,@img_contenttype )", connection );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> SqlParameter param0
> =3D new
> SqlParameter( "@img_name", SqlDbType.VarChar,50
> );<BR> param0.Value =
> imgName;<BR> command.Parameters.Add( param0
> );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> SqlParameter param1
> =3D new
> SqlParameter( "@img_data", SqlDbType.Image
> );<BR> param1.Value
> =3D imgbin;<BR> command.Parameters.Add( param1
> );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> SqlParameter param2
> =3D new
> SqlParameter( "@img_contenttype", SqlDbType.VarChar,50
> );<BR> param2.Value =
> imgcontenttype;<BR> command.Parameters.Add( param2
> );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial
> size=3D2> connection.Open();<BR> int
> numRowsAffected =
> command.ExecuteNonQuery();<BR> connection.Close();</FONT=
> ></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> return
> numRowsAffected;<BR> }<BR> }</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>UploadImage.aspx</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2><%@ Page language=3D"c#"
> Src=3D"UploadImage.aspx.cs"
> Inherits=3D"XXXXXX.UploadImage" CodeBehind=3D"UploadImage.aspx.cs"
> AutoEventWireup=3D"false" %><BR><!DOCTYPE HTML PUBLIC "-//W3C//DTD
> HTML 4.0
> Transitional//EN"
> ><BR><HTML><BR> <HEAD><BR> </HEAD><BR>&nbs=
> p;<body
> bgcolor=3D"#ffffff"><BR> <form
> enctype=3D"multipart/form-data"
> runat=3D"server" id=3D"form1"
> name=3D"form1"><BR> <h3><BR> &=
> nbsp;The
> ASPFree Friendly Image
> Uploader<BR> </h3><BR> Enter A
> Friendly
> Name<input type=3D"text" id=3D"txtImgName"
> runat=3D"server"><BR> <asp:RequiredFieldValidator
>
> id=3D"RequiredFieldValidator1" runat=3D"server"
> ErrorMessage=3D"Required"
> ControlToValidate=3D"txtImgName"></asp:RequiredFieldValidator><B=
> R> <br><BR> Select
> File To Upload: <input id=3D"UploadFile" type=3D"file"
> runat=3D"server"><BR> <asp:button
> id=3D"UploadBtn"
> Text=3D"Upload Me!" OnClick=3D"UploadBtn_Click"
> runat=3D"server"></asp:button><BR> </form><BR>&=
> nbsp; </FORM><BR> </body><BR></HTML><BR></FON=
> T></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>ViewImage.aspx.cs</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2> private void
> Page_Load(object sender,
> System.EventArgs e)<BR> {<BR> // Put user
> code to
> initialize the page here<BR> string ImageId =
> Request.QueryString["img"];<BR> string sqlText = "SELECT
> TestImg_Name,TestImg_data, TestImg_Type FROM TestImage WHERE TestImg_ID
> =3D " +
> ImageId;</FONT></DIV>
> <DIV> </DIV>
> <DIV><FONT face=3DArial
> size=3D2> //Response.Write(sqlText);<BR> &nbs=
> p;SqlConnection
> connection = new SqlConnection(
> "server=3Dlocalhost;uid=3DXXX;pwd=3DXXX;Database=3DXXXX"
> );<BR> SqlCommand command = new SqlCommand( sqlText,
> connection);<BR> <BR> connection.Open()=
> ;<BR> SqlDataReader
> dr = command.ExecuteReader();<BR> if (
> dr.Read()) <BR> {<BR> Respon=
> se.ContentType
> =3D
> dr["TestImg_Type"].ToString();<BR> Response.Binary=
> Write(
> (byte[]) dr["TestImg_Data"]
> );<BR> }<BR> <BR> &nbs=
> p;connection.Close();<BR> }</DIV></FONT>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV></BODY></HTML>
>
> ------=_NextPart_000_0088_01C14308.8ED2BEE0--