View Single Post
  #43 (permalink)  
Old April 16th, 2007, 07:49 AM
99mary 99mary is offline
Authorized User
 
Join Date: Mar 2007
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Choice avatar with loading on server if to whom will be necessary

UserProfile.ascx

<asp:FileUpload ID="filUpload" runat="server" />&nbsp;
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Add" CausesValidation="false" />

UserProfile.ascx.cs

       protected void btnUpload_Click(object sender, EventArgs e)
       {
           lblFeedbackKO.Visible = false;
           lblFeedbackOK.Visible = false;
           if (filUpload.PostedFile != null && filUpload.PostedFile.ContentLength > 0 && filUpload.PostedFile.FileName != string.Empty)
           {
               try
               {
                   // if not already present, create a directory named /Uploads/<CurrentUserName>
                   string dirUrl = (this.Page as MB.TheBeerHouse.UI.BasePage).BaseUrl +
                      "Uploads/" + this.Page.User.Identity.Name;
                   string dirPath = Server.MapPath(dirUrl);
                   if (!Directory.Exists(dirPath))
                       Directory.CreateDirectory(dirPath);
                   // save the file under the user's personal folder
                   string fileUrl = dirUrl + "/" + Path.GetFileName(filUpload.PostedFile.FileName);
                   string _inputFile = fileUrl;
                   string contentType = filUpload.PostedFile.ContentType;
                   int contentLength = filUpload.PostedFile.ContentLength;

                   if (contentType != "image/pjpeg" && contentType != "image/gif" && contentType != "image/jpeg" && contentType != "image/x-png")
                      {
                          lblFeedbackKO.Visible = true;
                          lblFeedbackKO.Text = "Only GIF, JPEG and PNG image types are supported.";

                      }
                   else

                          if (contentLength > 25000)
                          {
                              lblFeedbackKO.Visible = true;
                              lblFeedbackKO.Text = "Avatar images must be less than 25K in size.";

                          }
                          else
                          {
                              System.Drawing.Image image = System.Drawing.Image.FromStream(filUpload.PostedFi le.InputStream);
                              if (!((image.Width > 0 && image.Width <= 100) && (image.Height > 0 && image.Height <= 100)))
                              {
                                  lblFeedbackKO.Visible = true;
                                  lblFeedbackKO.Text = "Avatar images must be no larger than 150 by 150 pixels.";

                              }
                              else
                              {
                                  filUpload.PostedFile.SaveAs(Server.MapPath(fileUrl ));
                                  txtAvatarUrl.Text = fileUrl;
                                  lblFeedbackOK.Visible = true;
                                  lblFeedbackOK.Text = "File is successfully added.<br /> For change necessary to save profile.";
                              }
                          }

                  }

               catch (Exception ex)
               {
                   lblFeedbackKO.Visible = true;
                   lblFeedbackKO.Text = ex.Message;
               }
           }
       }