예제 #1
0
        protected void imgBtnUpload_Click(object sender, EventArgs e)
        {
            if (fileUpload1.HasFile)     // CHECK IF ANY FILE HAS BEEN SELECTED.
            {
                if (fileUpload1.PostedFile.ContentLength >= SystemConstants.ImageLimitInByte())
                {
                    lblUploadStatus.Text      = "Maximum file size is 4MB.";
                    lblUploadStatus.ForeColor = Color.Red;
                }
                else
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(fileUpload1.PostedFile.ContentType, "image/\\S+"))
                    {
                        string ext = fileUpload1.PostedFile.ContentType;
                        if (ext == "bmp" || ext == ".bmp")
                        {
                            lblUploadStatus.Text      = ".bmp extension is not supported";
                            lblUploadStatus.ForeColor = Color.Red;
                        }
                        else
                        {
                            int iFailedCnt = 0;

                            string sFileName = Path.GetFileName(fileUpload1.FileName);

                            if (SizeUploaded <= SystemConstants.MaxActivityImageStorage)
                            {
                                Stream input  = fileUpload1.PostedFile.InputStream;
                                byte[] buffer = new byte[input.Length];
                                using (MemoryStream ms = new MemoryStream())
                                {
                                    int read;
                                    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        ms.Write(buffer, 0, read);
                                    }
                                    if (isSupported)
                                    {
                                        UploadedTitle.Text            = "Preview";
                                        ProviderImagePreview.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(ms.ToArray());
                                    }
                                    else
                                    {
                                        ProviderImagePreview.Visible = false;
                                        lblimageTitle.Visible        = true;
                                        lblimageTitle.Text           = sFileName + " - " + SizeUploaded + "kb";
                                        UploadedTitle.Text           = "Uploaded Image";
                                    }
                                    hdnImageStream.Value = Convert.ToBase64String(ms.ToArray());
                                }

                                fileName = fileUpload1.FileName;

                                SizeUploaded   = fileUpload1.PostedFile.ContentLength / 1024;
                                ImageUploaded += 1;
                            }
                            else
                            {
                                iFailedCnt               += 1;
                                lblUploadStatus.Text     += "</br><b>" + iFailedCnt + "</b> file could not be uploaded. Maximum Size per activity is" + SystemConstants.MaxActivityImageStorage + " Kb";
                                lblUploadStatus.ForeColor = Color.Red;
                            }

                            lblUploadStatus.Text      = "Image uploaded.";
                            lblUploadStatus.ForeColor = ColorTranslator.FromHtml("#1B274F");
                        }
                    }
                    else
                    {
                        lblUploadStatus.Text = "Only image files are accepted."; lblUploadStatus.ForeColor = Color.Red;
                    }
                }
            }
            else
            {
                lblUploadStatus.Text = "No files selected."; lblUploadStatus.ForeColor = Color.Red;
            }
            lblUploadStatus.Visible = true;
            if (ImageUploaded > 0)
            {
                divUploadSuccessfull.Visible = true;
            }
        }