Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Uploading Images

0 views
Skip to first unread message

Palpy

unread,
Sep 22, 2001, 1:45:19 AM9/22/01
to
Hi All
I need to Upload an Image and saved in a DB and Display back again after reading from DB. I'm using Code Behind(c#). How to go about?Any examples/Articles?

Tks Palpy


--
Sent via http://csharpindex.com
C# Index to resources


Max Chen

unread,
Sep 22, 2001, 1:47:37 AM9/22/01
to
UploadImage.aspx.cs 
 
 public void UploadBtn_Click(object sender, System.EventArgs e)
  {
   if (Page.IsValid) //save the image
   {
    Stream imgStream = UploadFile.PostedFile.InputStream;
    int imgLen = UploadFile.PostedFile.ContentLength;
    string imgContentType = UploadFile.PostedFile.ContentType;
    string imgName = txtImgName.Value;
    byte[] imgBinaryData = new byte[imgLen];
    int n = imgStream.Read(imgBinaryData,0,imgLen);
    
    int RowsAffected = SaveToDB( imgName, imgBinaryData,imgContentType);
    if ( RowsAffected>0 )
    {
     Response.Write("<BR>The Image was saved");
    }
    else
    {
     Response.Write("<BR>An error occurred uploading the image");
    }
 
   }
  }
 
  private void InitializeComponent()
  {
   this.UploadBtn.Click += new System.EventHandler(this.UploadBtn_Click);
 
  }
 

  private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype)
  {
   //use the web.config to store the connection string
   SqlConnection connection = new SqlConnection("server=localhost;uid=Ursa;pwd=ursa;Database=UrsaTest");
   //SqlCommand command = new SqlCommand( "INSERT INTO TestImage (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )", connection );
   SqlCommand command = new SqlCommand( "INSERT INTO TestImage (TestImg_Name,TestImg_Data,TestImg_Type) VALUES ( @img_name, @img_data,@img_contenttype )", connection );
 
   SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
   param0.Value = imgName;
   command.Parameters.Add( param0 );
 
   SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
   param1.Value = imgbin;
   command.Parameters.Add( param1 );
 
   SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50 );
   param2.Value = imgcontenttype;
   command.Parameters.Add( param2 );
 
   connection.Open();
   int numRowsAffected = command.ExecuteNonQuery();
   connection.Close();
 
   return numRowsAffected;
  }
 }
 
UploadImage.aspx
 
<%@ Page language="c#" Src="UploadImage.aspx.cs" Inherits="XXXXXX.UploadImage" CodeBehind="UploadImage.aspx.cs" AutoEventWireup="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
 </HEAD>
 <body bgcolor="#ffffff">
  <form enctype="multipart/form-data" runat="server" id="form1" name="form1">
   <h3>
    The ASPFree Friendly Image Uploader
   </h3>
   Enter A Friendly Name<input type="text" id="txtImgName" runat="server">
   <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ControlToValidate="txtImgName"></asp:RequiredFieldValidator>
   <br>
   Select File To Upload: <input id="UploadFile" type="file" runat="server">
   <asp:button id="UploadBtn" Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"></asp:button>
  </form>
  </FORM>
 </body>
</HTML>
 
ViewImage.aspx.cs
 
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
   string ImageId = Request.QueryString["img"];
   string sqlText = "SELECT TestImg_Name,TestImg_data, TestImg_Type FROM TestImage WHERE TestImg_ID = " + ImageId;
 
   //Response.Write(sqlText);
   SqlConnection connection = new SqlConnection( "server=localhost;uid=XXX;pwd=XXX;Database=XXXX" );
   SqlCommand command = new SqlCommand( sqlText, connection);
   
   connection.Open();
   SqlDataReader dr = command.ExecuteReader();
   if ( dr.Read()) 
   {
    Response.ContentType = dr["TestImg_Type"].ToString();
    Response.BinaryWrite( (byte[]) dr["TestImg_Data"] );
   }
    
   connection.Close();
  }
 

Palpy

unread,
Sep 22, 2001, 12:11:49 PM9/22/01
to
Thanx

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&nbsp;</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;public void
> UploadBtn_Click(object sender,
> System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if
> (Page.IsValid)&nbsp;//save the
> image<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Stream imgStream
> =3D
> UploadFile.PostedFile.InputStream;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int imgLen
> =3D
> UploadFile.PostedFile.ContentLength;<BR>&nbsp;&nbsp;&nbsp;&nbsp;string
> imgContentType =
> UploadFile.PostedFile.ContentType;<BR>&nbsp;&nbsp;&nbsp;&nbsp;string
> imgName =
> txtImgName.Value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;byte[] imgBinaryData = new
> byte[imgLen];<BR>&nbsp;&nbsp;&nbsp;&nbsp;int n =
> imgStream.Read(imgBinaryData,0,imgLen);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&n=
> bsp;&nbsp;&nbsp;&nbsp;int
> RowsAffected = SaveToDB( imgName,
> imgBinaryData,imgContentType);<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (
> RowsAffected&gt;0
> )<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.=
> Write("&lt;BR&gt;The
> Image was
> saved");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>=
> &nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write=
> ("&lt;BR&gt;An
> error occurred uploading the
> image");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial
> size=3D2>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;private void
> InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.UploadBt=
> n.Click
> +=3D new System.EventHandler(this.UploadBtn_Click);</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;}</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><BR><FONT face=3DArial size=3D2>&nbsp;&nbsp;private int


> SaveToDB(string
> imgName, byte[] imgbin, string

> imgcontenttype)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//use the
> web.config to
> store the connection string<BR>&nbsp;&nbsp;&nbsp;SqlConnection
> connection = new
> SqlConnection("server=3Dlocalhost;uid=3DUrsa;pwd=3Dursa;Database=3DUrsaTe=
> st");<BR>&nbsp;&nbsp;&nbsp;//SqlCommand

> command = new SqlCommand( "INSERT INTO TestImage
> (img_name,img_data,img_contenttype) VALUES ( @img_name,
> @img_data,@img_contenttype )", connection

> );<BR>&nbsp;&nbsp;&nbsp;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>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;SqlParameter param0
> =3D new
> SqlParameter( "@img_name", SqlDbType.VarChar,50
> );<BR>&nbsp;&nbsp;&nbsp;param0.Value =
> imgName;<BR>&nbsp;&nbsp;&nbsp;command.Parameters.Add( param0
> );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;SqlParameter param1
> =3D new
> SqlParameter( "@img_data", SqlDbType.Image
> );<BR>&nbsp;&nbsp;&nbsp;param1.Value
> =3D imgbin;<BR>&nbsp;&nbsp;&nbsp;command.Parameters.Add( param1
> );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;SqlParameter param2
> =3D new
> SqlParameter( "@img_contenttype", SqlDbType.VarChar,50
> );<BR>&nbsp;&nbsp;&nbsp;param2.Value =
> imgcontenttype;<BR>&nbsp;&nbsp;&nbsp;command.Parameters.Add( param2
> );</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial
> size=3D2>&nbsp;&nbsp;&nbsp;connection.Open();<BR>&nbsp;&nbsp;&nbsp;int
> numRowsAffected =
> command.ExecuteNonQuery();<BR>&nbsp;&nbsp;&nbsp;connection.Close();</FONT=
> ></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;return
> numRowsAffected;<BR>&nbsp;&nbsp;}<BR>&nbsp;}</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>UploadImage.aspx</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&lt;%@ Page language=3D"c#"
> Src=3D"UploadImage.aspx.cs"
> Inherits=3D"XXXXXX.UploadImage" CodeBehind=3D"UploadImage.aspx.cs"
> AutoEventWireup=3D"false" %&gt;<BR>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD
> HTML 4.0
> Transitional//EN"
> &gt;<BR>&lt;HTML&gt;<BR>&nbsp;&lt;HEAD&gt;<BR>&nbsp;&lt;/HEAD&gt;<BR>&nbs=
> p;&lt;body
> bgcolor=3D"#ffffff"&gt;<BR>&nbsp;&nbsp;&lt;form
> enctype=3D"multipart/form-data"
> runat=3D"server" id=3D"form1"
> name=3D"form1"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;h3&gt;<BR>&nbsp;&nbsp;&nbsp;&=
> nbsp;The
> ASPFree Friendly Image
> Uploader<BR>&nbsp;&nbsp;&nbsp;&lt;/h3&gt;<BR>&nbsp;&nbsp;&nbsp;Enter A
> Friendly
> Name&lt;input type=3D"text" id=3D"txtImgName"
> runat=3D"server"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:RequiredFieldValidator
>
> id=3D"RequiredFieldValidator1" runat=3D"server"
> ErrorMessage=3D"Required"
> ControlToValidate=3D"txtImgName"&gt;&lt;/asp:RequiredFieldValidator&gt;<B=
> R>&nbsp;&nbsp;&nbsp;&lt;br&gt;<BR>&nbsp;&nbsp;&nbsp;Select
> File To Upload: &lt;input id=3D"UploadFile" type=3D"file"
> runat=3D"server"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;asp:button
> id=3D"UploadBtn"
> Text=3D"Upload Me!" OnClick=3D"UploadBtn_Click"
> runat=3D"server"&gt;&lt;/asp:button&gt;<BR>&nbsp;&nbsp;&lt;/form&gt;<BR>&=
> nbsp;&nbsp;&lt;/FORM&gt;<BR>&nbsp;&lt;/body&gt;<BR>&lt;/HTML&gt;<BR></FON=
> T></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>ViewImage.aspx.cs</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
> <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;private void
> Page_Load(object sender,
> System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;// Put user
> code to
> initialize the page here<BR>&nbsp;&nbsp;&nbsp;string ImageId =
> Request.QueryString["img"];<BR>&nbsp;&nbsp;&nbsp;string sqlText = "SELECT

> TestImg_Name,TestImg_data, TestImg_Type FROM TestImage WHERE TestImg_ID

> =3D " +
> ImageId;</FONT></DIV>
> <DIV>&nbsp;</DIV>
> <DIV><FONT face=3DArial
> size=3D2>&nbsp;&nbsp;&nbsp;//Response.Write(sqlText);<BR>&nbsp;&nbsp;&nbs=
> p;SqlConnection
> connection = new SqlConnection(
> "server=3Dlocalhost;uid=3DXXX;pwd=3DXXX;Database=3DXXXX"
> );<BR>&nbsp;&nbsp;&nbsp;SqlCommand command = new SqlCommand( sqlText,
> connection);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;connection.Open()=
> ;<BR>&nbsp;&nbsp;&nbsp;SqlDataReader
> dr = command.ExecuteReader();<BR>&nbsp;&nbsp;&nbsp;if (
> dr.Read())&nbsp;<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Respon=
> se.ContentType
> =3D
> dr["TestImg_Type"].ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;Response.Binary=


> Write(
> (byte[]) dr["TestImg_Data"]

> );<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbs=
> p;connection.Close();<BR>&nbsp;&nbsp;}</DIV></FONT>
> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV></BODY></HTML>
>
> ------=_NextPart_000_0088_01C14308.8ED2BEE0--

0 new messages