Esempio n. 1
0
 protected void UploadPicButton_Click(object sender, EventArgs e)
 {
     try
     {
         if (FileUpload1.HasFile && (FileUpload1.FileName.ToLower().EndsWith(".gif") || FileUpload1.FileName.ToLower().EndsWith(".jpg") ||
                                     FileUpload1.FileName.ToLower().EndsWith(".bmp") || FileUpload1.FileName.ToLower().EndsWith(".png")) &&
             FileUpload1.PostedFile.ContentLength <= 1048576)
         {
             string path      = @"~\Uploads\";
             string savedPath = Server.MapPath(path) + FileUpload1.FileName; //better for saving into IIS
             FileUpload1.SaveAs(savedPath);
             ////To test image dimensions
             //System.Drawing.Image i = System.Drawing.Image.FromFile(savedPath);
             //if (i.PhysicalDimension.Height != 200 || i.PhysicalDimension.Width != 200)
             //{
             //    FileUpload1.Dispose();
             //    i.Dispose();
             //    File.Delete(savedPath);
             //    throw new Exception("Incompatible dimensions. <br>Image must be 200px by 200px!");
             //}
             string    chkQuery   = DataProvider.LoggedInPage.getPictureAvailability((string)Session["User"]);
             DataTable dt         = HospitalClass.getDataTable(chkQuery);
             string    updateCode = "";
             if (dt.Rows.Count > 0)
             {
                 chkQuery = "update"; updateCode = "IMG_UPD";
             }
             else
             {
                 chkQuery = "insert"; updateCode = "IMG_UPL";
             }
             string        updaterId = UpdaterId();
             List <string> values    = new List <string>();
             values.Add((string)Session["User"]);
             values.Add(path + FileUpload1.FileName);
             values.Add(HospitalClass.getTransactionId());
             values.Add(updateCode);
             values.Add(updaterId);
             values.Add(chkQuery);
             int    status        = DataConsumer.executeProcedure("image_upload", values);
             string insertPicData = "update tobehospital.images set img=:image where user_id='" + (string)Session["User"] + "'";
             status = DataConsumer.sendPictureToDatabase(insertPicData, FileUpload1.FileBytes);
             ProfilePic.ImageUrl     = path + FileUpload1.FileName;
             UploadPicLabel.CssClass = "success";
             UploadPicLabel.Text     = "Done";
         }
         else
         {
             UploadPicLabel.CssClass = "error";
             if (!FileUpload1.HasFile)
             {
                 UploadPicLabel.Text = "Choose a picture";
             }
             else if (FileUpload1.PostedFile.ContentLength > 1048576)
             {
                 UploadPicLabel.Text = "Too Large";
             }
             else
             {
                 UploadPicLabel.Text = "Wrong Format";
             }
         }
     }
     catch (Exception ex)
     {
         UploadPicLabel.CssClass = "error";
         UploadPicLabel.Text     = "Error" + ex.Message;
         HospitalClass.Log(ex);
     }
 }