Click here to Skip to main content
Licence 
First Posted 20 Oct 2002
Views 415,932
Bookmarked 84 times

How to upload an image using ASP.NET

By | 20 Apr 2003 | Article
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

About the Author

Esteves

Web Developer

Brazil Brazil

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberabhishekabhishek0070:18 14 Mar '12  
Generalhmm Pinmemberrajesh243430:37 23 Apr '12  
QuestionHow to Move Image to specified location in asp.net Pinmembernaganesh19:07 28 Jul '10  
GeneralMy vote of 2 PinmemberAli Saeedi23:01 1 Feb '10  
GeneralI got it working in no time PinmemberWard Gannon11:54 17 Feb '09  
GeneralRe: I got it working in no time Pinmemberrajesh243430:50 23 Apr '12  
Questioni try alot but not get the solution PinmemberAtif Younas4:17 14 Jan '07  
QuestionHow to Upload Image to Ms Access by using Vb.Net? PinmemberGeoffreyOng16:44 7 Sep '06  
AnswerRe: How to Upload Image to Ms Access by using Vb.Net? PinmemberGeoffreyOng17:15 7 Sep '06  
QuestionHow to Upload Image to Ms Access? PinmemberGeoffreyOng16:40 7 Sep '06  
Generalthanks Pinmemberk_hammami20054:08 2 May '06  
GeneralImage upload and resize web control Pinmemberzioturo6:29 6 Nov '05  
GeneralRe: Image upload and resize web control Pinmemberkmeyer4:29 7 May '06  
QuestionASP.NET VB Pinmemberbartvn0:30 28 Oct '05  
AnswerRe: ASP.NET VB PinmemberNirali Desai7:15 30 Mar '06  
GeneralStore images in DB Pinmemberbartvn0:29 28 Oct '05  
GeneralWhy Store in DB Pinmemberbelfegor10:36 17 Aug '05  
GeneralRe: Why Store in DB PinmemberFoxtracker7:40 20 May '07  
GeneralVery Very Bad Article Pinmembervivekthangaswamy1:19 10 Aug '05  
GeneralVery Very Bad Article Pinmembervivekthangaswamy1:18 10 Aug '05  
GeneralRe: Very Very Bad Article PinmemberMarcel719:34 13 Dec '05  
GeneralCreate Report in WEB APPLICATION. Pinsusskjapanz22:14 13 Jul '05  
GeneralMultiple Selection of picture files PinsussMux9630:04 6 Jun '05  
GeneralHelp Me -- I want the Full Code Pinmembervivekthangaswamy7:02 27 Feb '05  
GeneralRe: Help Me -- I want the Full Code PinmemberEsteves12:43 8 Mar '05  

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.

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