Wednesday, May 9, 2012

ASP.NET(Upload Image on the server)


string path = HttpContext.Current.Request.PhysicalApplicationPath + @"Folder1\Folder2\Folder3\";

protected void btnPost_Click(object sender, EventArgs e)
    {
        try
        {
                #region Upload Image

                string FileName = "";
                bool FileOK = false;
                bool FileSaved = true;
                bool hasFile = false;

                if (Upload.HasFile)
                {
                    hasFile = true;
                    FileName = clsSession.ID + "_" + Upload.FileName;
                    String FileExtension = Path.GetExtension(FileName).ToLower();
                    String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (FileExtension == allowedExtensions[i])
                        {
                            FileOK = true;
                        }
                    }

                    if (FileOK)
                    {
                        try
                        {
                            Upload.PostedFile.SaveAs(path + FileName);
                            FileSaved = true;
                        }
                        catch (Exception ex)
                        {
                            lblError.Text = "File could not be uploaded." + ex.Message.ToString();
                            lblError.Visible = true;
                            FileSaved = false;
                        }
                    }
                    else
                    {
                        lblError.Text = "Cannot accept files of this type.";
                        lblError.Visible = true;
                    }
                }
                else
                {
                    lblError.Text = "No file selected";
                    lblError.Visible = true;
                }

                #endregion Upload Image

                #region Save Data into DB

                if (FileSaved)
                {
                    #region Get Max ID

                    string strSql = "SELECT Max(id) +1 id FROM TableName";
                    string strError = string.Empty;
                    SqlCommand command = new SqlCommand(strSql);
                    DALayer layer = new DALayer();
                    int ID = layer.GetSingleIntegerValue(command, ref strError);

                    if (strError != "T")
                    {
                        throw new Exception(strError);
                    }

                    #endregion Get Max ID

                    #region Insert into DB

                    strSql = "INSERT INTO TableName(Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field7)" +
                           " values (@Field1, @Field2 , @Field3 , @Field4 , @Field5 , GetDate(), GetDate(), 'A')";
                    command = new SqlCommand(strSql);

                    command.Parameters.AddWithValue("@Field1 " DBNull.Value );
                    command.Parameters.AddWithValue("@Field2 " DBNull.Value );
                    command.Parameters.AddWithValue("@Field3 ", DBNull.Value );
                    command.Parameters.AddWithValue("@Field4 " DBNull.Value );

                    if (hasFile)
                        command.Parameters.AddWithValue("@Field5 ", FileName);
                    else
                        command.Parameters.AddWithValue("@Field5 ", DBNull.Value);

                    layer = new DALayer();
                    layer.ExecuteQuery(command, ref strError);
                    if (strError != "T")
                    {
                        throw new Exception(strError);
                    }
                    else
                    {
                        txtBlogTitle.Text = "";
                        txtContent.Text = "";
                        runjQueryCode("Notification!", " Saved Successfully!");
                    }

                    #endregion Insert into DB
                }

                #endregion Save Data into DB
            }
        catch (Exception ae)
        {
            runjQueryCode("Not Saved", ae.Message);
        }

      
    }

No comments:

Post a Comment