Exemplo n.º 1
0
        /// <summary>
        /// Page load.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Arguments</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack && imageFile.PostedFile != null && imageFile.PostedFile.ContentLength > 0)
            {
                string imageName = Path.GetFileName(imageFile.PostedFile.FileName);
                string imageNameWithoutExtension = Path.GetFileNameWithoutExtension(imageFile.PostedFile.FileName);
                string imageExtension = Path.GetExtension(imageFile.PostedFile.FileName).Remove(0, 1);

                if (Tools.In(imageExtension, VALID_IMAGE_FILE_EXTENSIONS))
                {
                    int imageDataLength = (int)imageFile.PostedFile.InputStream.Length;
                    byte[] imageData = new byte[imageDataLength];
                    imageFile.PostedFile.InputStream.Read(imageData, 0, imageDataLength);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(imageFile.PostedFile.InputStream);
                    ArticleImage newImage = new ArticleImage(-1, imageNameWithoutExtension, imageExtension, imageName, image.Width, image.Height, imageData);
                    int imageId = DataModule.SaveImage(newImage, Authentication.CurrentUser.UserLogin);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "sendImageIdToParent", string.Format(@"window.parent.insertImage('{0}', '{1}');", imageId, imageTitle.Value), true);
                }
                else
                {
                    hdnInvalidExtension.Value = "true";
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves an image in database and returns its ID.
        /// </summary>
        /// <param name="image">The image to persist in database.</param>
        /// <param name="user">The user that uploaded the image.</param>
        /// <returns>The database ID of the newly saved image.</returns>
        public static int SaveImage(ArticleImage image, string user)
        {
            int imageId = -1;
            string query = string.Format(@"EXEC InsertImage '{0}', '{1}', '{2}', {3}, {4}, '{5}', @data",
                image.NameWithoutExtension, image.Extension, image.Title, image.Width, image.Height, user);

            using (SqlConnection connection = DataAccess.GetSqlConnection())
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    SqlParameter imageParameter = new SqlParameter();
                    imageParameter.SqlDbType = SqlDbType.Image;
                    imageParameter.ParameterName = "data";
                    imageParameter.Value = image.Data;
                    command.Parameters.Add(imageParameter);
                    imageId = Convert.ToInt32(command.ExecuteScalar());
                }
            }
            return imageId;
        }