Using Upload (Async) to save images

4 posts, 0 answers
  1. Edd
    Edd avatar
    37 posts
    Member since:
    Jan 2013

    Posted 08 Jan 2014 Link to this post

    Please how can ii use  upload(Async) to select images onto a form and then save it into a database for later retrieval.
    Any help or idea. 
    Thanks in advance
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Jan 2014 Link to this post

    Hi Edd,

    Please have a look into the following sample code snippet which works fine at my end. 

    ASPX:
    <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" TargetFolder="~/Uploadimages"
        AllowedFileExtensions=".jpg" OnFileUploaded="RadAsyncUpload1_FileUploaded">
    </telerik:RadAsyncUpload>
    <telerik:RadButton runat="server" Text="Postback" ID="RadButton1">
    </telerik:RadButton>
    <telerik:RadButton ID="RadButton2" runat="server" Text="GetImage" OnClick="RadButton2_Click">
    </telerik:RadButton>

    C#:
    protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        Stream file = e.File.InputStream;
        byte[] data = new byte[file.Length];
        file.Read(data, 0, Convert.ToInt32(file.Length));
        String connectionstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(connectionstring);
        try
        {
            sqlconn.Open();
            //inserting the image into db
            SqlCommand command = new SqlCommand("Insert into Pictable (msgid, pic1) Values (6, @pic)", sqlconn);
            command.Parameters.Add("@pic", SqlDbType.VarBinary).Value = data;
            command.ExecuteNonQuery();
        }
        finally
        {
            file.Close();
            sqlconn.Close();
        }
    }
        protected void RadButton2_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
                {
                    //accessing image from database
                    const string SQL = "SELECT [pic1] FROM [Pictable] ";
                    SqlCommand myCommand = new SqlCommand(SQL, myConnection);
                    myConnection.Open();
                    SqlDataReader myReader = myCommand.ExecuteReader();
                    if (myReader.Read())
                    {
     
                        Response.BinaryWrite((byte[])myReader["pic1"]);
                    }
                    myReader.Close();
                    myConnection.Close();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Edd
    Edd avatar
    37 posts
    Member since:
    Jan 2013

    Posted 09 Jan 2014 Link to this post

    Please i used the exact code you provided changing the connectionstrings to suit mine but didnt work.
     below are the codes

    ASPX
    <body>
        <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" TargetFolder="~/Uploadimages"
        AllowedFileExtensions=".jpg" OnFileUploaded="RadAsyncUpload1_FileUploaded">
    </telerik:RadAsyncUpload>
    <telerik:RadButton runat="server" Text="Postback" ID="RadButton1">
    </telerik:RadButton>
    <telerik:RadButton ID="RadButton2" runat="server" Text="GetImage" OnClick="RadButton2_Click">
    </telerik:RadButton>
        </div>
        </form>
     
         
    </body>


    C#

     
    protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            Stream file = e.File.InputStream;
            byte[] data = new byte[file.Length];
            file.Read(data, 0, Convert.ToInt32(file.Length));
            String connectionstring = WebConfigurationManager.ConnectionStrings["RealEstateConnectionString1"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(connectionstring);
            try
            {
                sqlconn.Open();
                //inserting the image into db
                SqlCommand command = new SqlCommand("Insert into Pictable (pic1) Values (@pic)", sqlconn);
                command.Parameters.Add("@pic", SqlDbType.VarBinary).Value = data;
                command.ExecuteNonQuery();
            }
            finally
            {
                file.Close();
                sqlconn.Close();
            }
        }
        protected void RadButton2_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RealEstateConnectionString1"].ConnectionString))
                {
                    //accessing image from database
                    const string SQL = "SELECT [pic1] FROM [Pictable] ";
                    SqlCommand myCommand = new SqlCommand(SQL, myConnection);
                    myConnection.Open();
                    SqlDataReader myReader = myCommand.ExecuteReader();
                    if (myReader.Read())
                    {
     
                        Response.BinaryWrite((byte[])myReader["pic1"]);
                    }
                    myReader.Close();
                    myConnection.Close();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

    Database Script
    USE [RealEstate]
    GO
     
    /****** Object:  Table [dbo].[Pictable]    Script Date: 1/9/2014 12:13:34 PM ******/
    SET ANSI_NULLS ON
    GO
     
    SET QUOTED_IDENTIFIER ON
    GO
     
    CREATE TABLE [dbo].[Pictable](
        [msgid] [int] IDENTITY(1,1) NOT NULL,
        [pic1] [image] NULL,
     CONSTRAINT [PK_Pictable] PRIMARY KEY CLUSTERED
    (
        [msgid] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
     
    GO
     
    Thank you...
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Jan 2014 Link to this post

    Hi Edd,

    Unfortunately I couldn't replicate the issue at my end. The given code was working fine at my end.

    Thanks,
    Shinu
Back to Top