protected void ibtnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                if (IsImageFile((HttpPostedFile)FileUpload1.PostedFile))
                {
                    try
                    {
                        TheCancerProject.Core.Investigation investigation = new TheCancerProject.Core.Investigation();
                        System.IO.Stream       fs = FileUpload1.PostedFile.InputStream;
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        Byte[] img = br.ReadBytes((Int32)fs.Length);

                        investigation.Image       = img;
                        investigation.Summary     = txtSummary.Text;
                        investigation.DateCreated = DateTime.Now;
                        InvestigationDAO.Save(investigation);

                        string base64String = Convert.ToBase64String(img, 0, img.Length);
                        imgPicture.ImageUrl = "data:image/png;base64," + base64String;
                        imgPicture.Visible  = true;
                    }
                    catch (Exception ex)
                    {
                        lblErrorMsg.Text = "Error Occurred, Cannot Upload!";
                        throw;
                    }
                }
                else
                {
                    lblErrorMsg.Visible = true;
                    lblErrorMsg.Text    = "Invalid File, Cannot Upload!";
                }
            }
            else
            {
                lblErrorMsg.Visible = true;
                lblErrorMsg.Text    = "Please select a File";
            }
        }
Пример #2
0
        protected void ibtnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload1.HasFile)
                {
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    //if (IsImageFile((HttpPostedFile)FileUpload1.PostedFile))
                    if (theFile.FileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) || theFile.FileName.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase) || theFile.FileName.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase) || theFile.FileName.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
                    {
                        try
                        {
                            TheCancerProject.Core.Investigation investigation = new TheCancerProject.Core.Investigation();
                            //System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
                            //System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                            //Byte[] img = br.ReadBytes((Int32)fs.Length);
                            byte[] img = new byte[FileUpload1.PostedFile.ContentLength];

                            if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.InputStream.Position == 0)
                            {
                                FileUpload1.PostedFile.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
                            }

                            investigation.Image       = img;
                            investigation.Summary     = txtSummary.Text;
                            investigation.DateCreated = DateTime.Now;
                            investigation.DateUpdated = DateTime.Now;
                            InvestigationDAO.Save(investigation);


                            string base64String = Convert.ToBase64String(img, 0, img.Length);
                            imgPicture.ImageUrl = "data:image/png;base64," + base64String;
                            imgPicture.Visible  = true;

                            lblDateUploaded.Visible = true;
                            lblDateUploaded.Text   += investigation.DateCreated.ToString("dd/MM/yyyy");
                        }
                        catch (Exception ex)
                        {
                            lblErrorMsg.Text = "Error Occurred, Cannot Upload!";
                            throw;
                        }
                    }
                    else
                    {
                        lblErrorMsg.Visible = true;
                        lblErrorMsg.Text    = "Invalid File, Cannot Upload!";
                    }
                }
                else
                {
                    lblErrorMsg.Visible = true;
                    lblErrorMsg.Text    = "Please select a File";
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
                if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "message"))
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", @"<script type='text/javascript'>alertify.alert('Message', """ + errorMessage.Replace("\n", "").Replace("\r", "") + @""", function(){});</script>", false);
                }
            }
        }