Click here to Skip to main content
11,489,693 members (69,270 online)
Click here to Skip to main content

How to upload an image using ASP.NET

, 20 Apr 2003 507.4K 14K 85
Rate this:
Please Sign up or sign in to vote.
How to upload an image using ASP.NET for beginners

Introduction

This article is for beginners who want to learn how to upload images.

Step 1

First add the following HTML tag :-

<input id="Upload" style="Z-INDEX: 102; LEFT: 104px; WIDTH: 288px; 
  POSITION: absolute; TOP: 64px; HEIGHT: 22px" type="file" 
  size="28" name="Upload" runat="server"> 

This control will upload your image.

Step 2

Create a Button called "Upload" and another called "LoadImage".  Add the DataGrid and do required bindings.

Step 3

Lets see some sample code now.

    private void Button1_Click(object sender, System.EventArgs e)
    {
        //It verifies if the archive exists 
        if (Upload.PostedFile != null)
        {
            //To create a PostedFile
            HttpPostedFile File = Upload.PostedFile;

            //Create byte Array with file len
            byte[] Data = new Byte[File.ContentLength];
            //force the control to load data in array
            File.InputStream.Read(Data,0,File.ContentLength);
                
            int i = 0;

            //Dysplay array data in textbox
            for (i=0;i<Data.Length;i++)
            {
                TextBox1.Text += Data[i].ToString();  
            }

            //Create procedure parameter
            object[] obj = new object[1]; 

            obj[0] = Data;

            //Execute the procedure with Microsoft.ApplicationBlocks.Data
            //Simple procedure
            /*CREATE PROCEDURE sp_img(@img image)  AS

            insert into tb_img values(@img)*/

            //record data
            SqlHelper.ExecuteNonQuery(connectionString,"sp_img",obj);       

        }
    }

    private void Button2_Click(object sender, System.EventArgs e)
    {
        //Bind data to your grid
        //more details consulting
        //Working DataGrid TemplateColumns and Bind this Columns 
        DataSet ds = SqlHelper.ExecuteDataset(connectionString,
            "sp_load_img",null);
        grid.DataSource  = ds.Tables[0].DefaultView;
        grid.ObjectName = "Image1";
        grid.FieldName = "img";
        grid.Editable = false;
        grid.DataBind();
        grid.DataBindObjects(grid,ds,0,grid.PageSize);  

        int i =0;

        for (i=0;i<ds.Tables[0].Rows.Count;i++)
        {
                        //test your bitmap is valid 
                        //Demonstration
            byte[] bits = (byte[]) ds.Tables[0].Rows[i]
                ["img"];  
            MemoryStream memorybits = new MemoryStream(bits);
            Bitmap bitmap = new Bitmap(memorybits);
        }

 
    }

    private void grid_PageIndexChanged(object source, 
           System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        //Page your grid
        //Bind data to your grid
        //more details consulting
        //Working DataGrid TemplateColumns and Bind this Columns 
        grid.CurrentPageIndex =e.NewPageIndex; 
        DataSet ds = SqlHelper.ExecuteDataset(connectionString,
            "sp_load_img",null);
        grid.DataSource  = ds.Tables[0].DefaultView;
        grid.ObjectName = "Image1";
        grid.FieldName = "img";
        grid.Editable = false;
        grid.DataBind();
        grid.DataBindObjects(grid,ds,e.NewPageIndex,grid.PageSize);  
        
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Esteves
Web Developer
Brazil Brazil
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pin
Karan M31-Mar-14 2:33
memberKaran M31-Mar-14 2:33 
GeneralMy vote of 1 Pin
Josh_T2-Apr-13 20:22
memberJosh_T2-Apr-13 20:22 
Questionupload file Pin
ceryhel8-Feb-13 17:33
memberceryhel8-Feb-13 17:33 
Questionhow to picture upload and show Pin
Ramita De12-Jun-12 1:50
memberRamita De12-Jun-12 1:50 
AnswerRe: how to picture upload and show Pin
Corruptmind26-Jun-12 0:49
memberCorruptmind26-Jun-12 0:49 
AnswerRe: how to picture upload and show Pin
shanid3608-Aug-12 20:07
membershanid3608-Aug-12 20:07 
GeneralMy vote of 5 Pin
abhishekabhishek00714-Mar-12 1:18
memberabhishekabhishek00714-Mar-12 1:18 
Generalhmm Pin
rajesh2434323-Apr-12 1:37
memberrajesh2434323-Apr-12 1:37 
QuestionHow to Move Image to specified location in asp.net Pin
naganesh28-Jul-10 20:07
membernaganesh28-Jul-10 20:07 
GeneralMy vote of 2 Pin
Ali Saeedi2-Feb-10 0:01
memberAli Saeedi2-Feb-10 0:01 
GeneralI got it working in no time Pin
Ward Gannon17-Feb-09 12:54
memberWard Gannon17-Feb-09 12:54 
GeneralRe: I got it working in no time Pin
rajesh2434323-Apr-12 1:50
memberrajesh2434323-Apr-12 1:50 
Questioni try alot but not get the solution Pin
Atif Younas14-Jan-07 5:17
memberAtif Younas14-Jan-07 5:17 
QuestionHow to Upload Image to Ms Access by using Vb.Net? Pin
GeoffreyOng7-Sep-06 17:44
memberGeoffreyOng7-Sep-06 17:44 
AnswerRe: How to Upload Image to Ms Access by using Vb.Net? Pin
GeoffreyOng7-Sep-06 18:15
memberGeoffreyOng7-Sep-06 18:15 
QuestionHow to Upload Image to Ms Access? Pin
GeoffreyOng7-Sep-06 17:40
memberGeoffreyOng7-Sep-06 17:40 
Generalthanks Pin
k_hammami20052-May-06 5:08
memberk_hammami20052-May-06 5:08 
GeneralImage upload and resize web control Pin
zioturo6-Nov-05 7:29
memberzioturo6-Nov-05 7:29 
GeneralRe: Image upload and resize web control Pin
kmeyer7-May-06 5:29
memberkmeyer7-May-06 5:29 
QuestionASP.NET VB Pin
bartvn28-Oct-05 1:30
memberbartvn28-Oct-05 1:30 
AnswerRe: ASP.NET VB Pin
Nirali Desai30-Mar-06 8:15
memberNirali Desai30-Mar-06 8:15 
GeneralStore images in DB Pin
bartvn28-Oct-05 1:29
memberbartvn28-Oct-05 1:29 
GeneralWhy Store in DB Pin
belfegor17-Aug-05 11:36
memberbelfegor17-Aug-05 11:36 
GeneralRe: Why Store in DB Pin
Foxtracker20-May-07 8:40
memberFoxtracker20-May-07 8:40 
GeneralVery Very Bad Article Pin
vivekthangaswamy10-Aug-05 2:19
membervivekthangaswamy10-Aug-05 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 21 Apr 2003
Article Copyright 2002 by Esteves
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid