Explanation:
AS a First step we will create Our
UploadImage.aspx File;
1 <%@ Page language="c#" Src="UploadImage.aspx.cs" Inherits="DBImages.UploadImage"
%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3 <HTML>
4 <body bgcolor=#ffffff>
5 <form enctype="multipart/form-data" runat=server id=form1 name=form1>
6 <h3>Upload your Image</h3>
7 Enter A Friendly Name<input type=text id=txtImgName runat="server" >
8 <asp:RequiredFieldValidator id=RequiredFieldValidator1 runat="server" ErrorMessage="Required"
ControlToValidate="txtImgName"></asp:RequiredFieldValidator>
9 <br>Select File To Upload:
10 <input id="UploadFile" type=file runat=server>
11 <asp:button id=UploadBtn Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"></asp:button>
12 </form>
13 </body>
14 </HTML>
Now let us see what is happning in the code;
Line 1 loads our code-behind script, called UploadImage.aspx.cs (given
below).
Line 5 uses the "multipart/form-data" encoding type for the <form>
tag, telling the browser that a large amount of binary (image) data will be returned
by the form.
Line 8 uses the .NET RequiredFieldValidator web control. It requires the user to
enter a friendly name for the image. If the user attempts to leave this field blank,
the script will tell the user that a friendly name is required. Depending on what
you're using the script to do, you may not even need the information in this field;
it simply provides a reference to the images in a more friendly context, such as
for an image library or picture album.
Line 10 uses the HtmlInputFile control. This control is part of the HTML controls
library for .NET and is basically a fancy text box control that contains a Browse
button; it knows that the value it will receive is a binary file.
Line 11 is the Button web control, which calls a function named UploadBTn_Click
when the button for the control is clicked.
The Code-Behind (.cs) Script;
1 using System;
2 using System.Configuration;
3 using System.Data;
4 using System.Data.SqlClient;
5 using System.Web;
6 using System.IO;
7 using System.Web.SessionState;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11 namespace DBImages
12 {
13 public class UploadImage : System.Web.UI.Page
14 {
15 protected System.Web.UI.WebControls.Button UploadBtn;
16 protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
17 protected System.Web.UI.HtmlControls.HtmlInputText txtImgName;
18 protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
19 public UploadImage() { }
20 private void Page_Load(object sender, System.EventArgs e){ }
21 public void UploadBtn_Click(object sender, System.EventArgs e)
22 {
23 if (Page.IsValid) //save the image
24 {
25 Stream imgStream = UploadFile.PostedFile.InputStream;
26 int imgLen = UploadFile.PostedFile.ContentLength;
27 string imgContentType = UploadFile.PostedFile.ContentType;
28 string imgName = txtImgName.Value;
29 byte[] imgBinaryData = new byte[imgLen];
30 int n = imgStream.Read(imgBinaryData,0,imgLen);
31 String idis = Request.QueryString["id"];
32 int busid = System.Convert.ToInt32(idis);
33 int RowsAffected = SaveToDB( imgName, imgBinaryData,imgContentType, busid);
34 if ( RowsAffected>0 )
35 {
36 Response.Write("<BR>The Image was saved");
37 }
38 else
39 {
40 Response.Write("<BR>An error occurred uploading the image");
41 }
42 }
43 }
44 private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype, int
busid)
45 {
46 //use the web.config to store the connection string
47 SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
48 SqlCommand command = new SqlCommand( "INSERT INTO Image (img_name,img_data,img_contenttype,business_id)
VALUES ( @img_name, @img_data,@img_contenttype,@img_busid)", connection );
49 SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
50 param0.Value = imgName;
51 command.Parameters.Add( param0 );
52 SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
53 param1.Value = imgbin;
54 command.Parameters.Add( param1 );
55 SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50);
56 param2.Value = imgcontenttype;
57 command.Parameters.Add( param2 );
58 SqlParameter param3 = new SqlParameter( "@img_busid", SqlDbType.Int,4 );
59 param3.Value = busid;
60 command.Parameters.Add( param3 );
61 connection.Open();
62 int numRowsAffected = command.ExecuteNonQuery();
63 connection.Close();
64 return numRowsAffected;
65 }
66 }
67 }
Where DSN is our Connection string to the MS SQL Database. You noticed that our
lives got easy with the usage of Parameters.
|