65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ASP.NET 上传图像

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.41/5 (23投票s)

2002年10月21日

viewsIcon

593590

downloadIcon

14775

使用 ASP.NET 上传图像的初学者指南

引言

本文面向希望学习如何上传图像的初学者。

步骤 1

首先添加以下 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"> 

此控件将上传您的图像。

第二步

创建一个名为“Upload”(上传)的 Button ,另一个名为“LoadImage”(加载图像)。 添加 DataGrid 并进行必要的绑定。

步骤 3

现在我们来看一些示例代码。

    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);  
        
    }
}
© . All rights reserved.