Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET
Article

How to upload an image using ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.41/5 (23 votes)
20 Apr 2003 588.9K   14.8K   84   40
How to upload an image using ASP.NET for beginners

Image 1

Introduction

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

Step 1

First add the following HTML tag :-

HTML
<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.

C#
    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


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: ASP.NET VB Pin
Nirali Desai30-Mar-06 7:15
Nirali Desai30-Mar-06 7:15 
GeneralStore images in DB Pin
bartvn28-Oct-05 0:29
bartvn28-Oct-05 0:29 
GeneralWhy Store in DB Pin
belfegor17-Aug-05 10:36
belfegor17-Aug-05 10:36 
GeneralRe: Why Store in DB Pin
Foxtracker20-May-07 7:40
Foxtracker20-May-07 7:40 
GeneralVery Very Bad Article Pin
vivekthangaswamy10-Aug-05 1:19
professionalvivekthangaswamy10-Aug-05 1:19 
GeneralVery Very Bad Article Pin
vivekthangaswamy10-Aug-05 1:18
professionalvivekthangaswamy10-Aug-05 1:18 
GeneralRe: Very Very Bad Article Pin
Marcel7113-Dec-05 9:34
Marcel7113-Dec-05 9:34 
GeneralCreate Report in WEB APPLICATION. Pin
kjapanz13-Jul-05 22:14
kjapanz13-Jul-05 22:14 
Can you help me to solve this problem?

How should i do to create a report in a web application which in that report contains several images and a short briefcast about the material that i must report in?

I am using VB.NET and ASP.NET with SQL SERVER 2000.

Thanks,

Smile | :)
GeneralMultiple Selection of picture files Pin
Member 26508896-Jun-05 0:04
Member 26508896-Jun-05 0:04 
GeneralHelp Me -- I want the Full Code Pin
vivekthangaswamy27-Feb-05 7:02
professionalvivekthangaswamy27-Feb-05 7:02 
GeneralRe: Help Me -- I want the Full Code Pin
Esteves8-Mar-05 12:43
Esteves8-Mar-05 12:43 
QuestionWhat about VB .Net ? please :( Pin
Hasan Bazerbashi23-Apr-04 5:45
Hasan Bazerbashi23-Apr-04 5:45 
AnswerRe: What about VB .Net ? please :( Pin
manueell21-Sep-04 7:00
manueell21-Sep-04 7:00 
GeneralDB Question Pin
Steven Carleton29-Jun-03 12:10
Steven Carleton29-Jun-03 12:10 
GeneralRe: DB Question Pin
Esteves29-Jun-03 12:38
Esteves29-Jun-03 12:38 
GeneralRe: DB Question Pin
knowon7-Mar-05 20:57
sussknowon7-Mar-05 20:57 
GeneralRe: DB Question Pin
Esteves8-Mar-05 12:42
Esteves8-Mar-05 12:42 
GeneralRe: DB Question(Important pls reply) Pin
Nisha_D6-Mar-07 22:13
Nisha_D6-Mar-07 22:13 
GeneralDoes not compile Pin
jeffkararo21-Apr-03 2:14
jeffkararo21-Apr-03 2:14 
GeneralRe: Does not compile Pin
Esteves21-Apr-03 8:29
Esteves21-Apr-03 8:29 

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

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