Re: Ajax based user profile not reloading image

  •  06-26-2009, 12:46 PM

    Re: Ajax based user profile not reloading image

    Do you mean you want to implement the same function as our example ?
     
    You can check our code ! the ajaxuplader.zip already contains it.
     
    Check the folder AdvancedCS20-NoAjax
     
    1. <%@ Page Language="C#" MasterPageFile="~/NoAjax.master" Title="Ajax based user profile" %>  
    2. <script runat="server">  
    3.   
    4.     protected override void OnLoad(EventArgs e)   
    5.     {   
    6.         base.OnLoad(e);   
    7.         if (!IsPostBack)   
    8.         {   
    9.             LoadView();   
    10.         }   
    11.     }   
    12.   
    13.     private void LoadView()   
    14.     {   
    15.         DataRow row = SampleDB.GetCurentUserRow();   
    16.   
    17.         TextBoxSignature.Text = row["Signature"].ToString();   
    18.         TextBoxDescription.Text = row["Description"].ToString();   
    19.         string photoname = row["PhotoTempFileName"].ToString();   
    20.         if (string.IsNullOrEmpty(photoname))   
    21.         {   
    22.             ImagePhoto.ImageUrl = "~/sampleimages/anonymous.gif";   
    23.         }   
    24.         else   
    25.         {   
    26.             ImagePhoto.ImageUrl = "UserPhoto.ashx?User=" + row["UserName"] + "&_hash=" + photoname.GetHashCode();   
    27.         }   
    28.     }   
    29.   
    30.     double MaxWidth = 160.0;   
    31.     double MaxHeight = 120.0;   
    32.   
    33.     protected void UploadPhoto_FileUploaded(object sender, UploaderEventArgs args)   
    34.     {       
    35.         try   
    36.         {   
    37.             System.Drawing.Bitmap img;   
    38.             using (Stream stream = args.OpenStream())   
    39.             {   
    40.                 img = new System.Drawing.Bitmap(stream);   
    41.             }   
    42.             using (img)   
    43.             {   
    44.                 if (img.Width > MaxWidth || img.Height > MaxHeight)   
    45.                 {   
    46.                     double scale = Math.Max(img.Width / MaxWidth, img.Height / MaxHeight);   
    47.                     int w = (int)(img.Width / scale);   
    48.                     int h = (int)(img.Height / scale);   
    49.                     //System.Drawing.Image.GetThumbnailImageAbort   
    50.   
    51.                     try   
    52.                     {   
    53.                         using (System.Drawing.Image thumb = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb))   
    54.                         {   
    55.                             using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumb))   
    56.                             {   
    57.                                 g.DrawImage(img   
    58.                                     , new System.Drawing.Rectangle(0, 0, w, h)   
    59.                                     , new System.Drawing.Rectangle(0, 0, img.Width, img.Height)   
    60.                                     , System.Drawing.GraphicsUnit.Pixel);   
    61.                             }   
    62.   
    63.                             thumb.Save(args.GetTempFilePath(), System.Drawing.Imaging.ImageFormat.Png);   
    64.                             LabelPhotoError.Text = "The image is uploaded and resized.";   
    65.                         }   
    66.   
    67.                     }   
    68.                     catch (Exception x)   
    69.                     {   
    70.                         LabelPhotoError.Text = w + ":" + h + ":" + x.ToString();   
    71.                         args.Delete();   
    72.                     }   
    73.                 }   
    74.             }   
    75.         }   
    76.         catch (Exception x)   
    77.         {   
    78.             LabelPhotoError.Text = x.ToString();   
    79.             args.Delete();   
    80.         }   
    81.     }   
    82.   
    83.     protected void UploadPhoto_FileChanged(object sender, PersistedFileEventArgs args)   
    84.     {   
    85.         ImagePhoto.ImageUrl = "ShowUploadPhoto.ashx?Guid=" + args.FileGuid;   
    86.     }   
    87.   
    88.     protected void ButtonUpdate_Click(object sender, EventArgs e)   
    89.     {   
    90.         int userid = SampleDB.GetCurrentUserId();   
    91.   
    92.         if (UploadPhoto.File != null)   
    93.         {   
    94.             DataRow row = SampleDB.GetCurentUserRow();   
    95.             string oldphotoname = row["PhotoTempFileName"].ToString();   
    96.   
    97.             string filedir = SampleUtil.GetFileDirectory();   
    98.             string newfilename = "photo." + UploadPhoto.File.FileGuid + "." + UploadPhoto.File.FileName + ".resx";   
    99.             UploadPhoto.File.MoveTo(Path.Combine(filedir, newfilename));   
    100.             SampleDB.ExecuteNonQuery("UPDATE UploaderUsers SET Signature={1},Description={2},PhotoTempFileName={3} WHERE UserId={0}"   
    101.             , userid, TextBoxSignature.Text, TextBoxDescription.Text, newfilename);   
    102.   
    103.             if (!string.IsNullOrEmpty(oldphotoname))   
    104.             {   
    105.                 string oldphotopath = Path.Combine(filedir, oldphotoname);   
    106.                 if (File.Exists(oldphotopath))   
    107.                 {   
    108.                     File.Delete(oldphotopath);   
    109.                 }   
    110.             }   
    111.         }   
    112.         else   
    113.         {   
    114.             SampleDB.ExecuteNonQuery("UPDATE UploaderUsers SET Signature={1},Description={2} WHERE UserId={0}"   
    115.             , userid, TextBoxSignature.Text, TextBoxDescription.Text);   
    116.         }   
    117.   
    118.         SampleDB.ExecuteNonQuery("UPDATE UploaderUsers SET IPAddress={1} WHERE UserId={0}", userid, Context.Request.UserHostAddress);   
    119.   
    120.         SampleDB.ResetCurrentUserRow();   
    121.   
    122.         LoadView();   
    123.   
    124.         int time = 0;   
    125.         if (ViewState["SaveOK"] != null)   
    126.             time = (int)ViewState["SaveOK"];   
    127.         time++;   
    128.         ViewState["SaveOK"] = time;   
    129.   
    130.         ButtonUpdate.Text = "Update successfully " + new string('!', time);   
    131.     }   
    132.     </script>  
    133. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
    134.     <h2>Ajax based user profile</h2>  
    135.     <p>User profile is the primary part of a community application. Ever wanted to allow users update the images of their profiles using AJAX without reloading the page? Test it now! </p>  
    136.     <table cellpadding="2" cellspacing="2">  
    137.         <tr>  
    138.             <td valign="top">Image:   
    139.             </td>  
    140.             <td valign="top"> <asp:Image ID="ImagePhoto" runat="server" CssClass="ImagePhoto" /> <br />  
    141.             <CuteWebUI:UploadPersistedFile runat="server" ID="UploadPhoto" DirtyText="(not saved)"  
    142.             InsertText="Change the profile Image" OnFileUploaded="UploadPhoto_FileUploaded" OnFileChanged="UploadPhoto_FileChanged"  
    143.             ItemTextTemplate="<br/>{0} {1} ({2})">  
    144.             <ValidateOption AllowedFileExtensions="jpg,jpeg,gif,png" />  
    145.         </CuteWebUI:UploadPersistedFile>  
    146.             </td>  
    147.         </tr>  
    148.         <tr>  
    149.             <td valign="top">Description:   
    150.             </td>  
    151.             <td valign="top"><asp:TextBox ID="TextBoxDescription" runat="server" Height="50px" TextMode="MultiLine"  
    152.             Width="250px"></asp:TextBox>  
    153.             </td>  
    154.         </tr>  
    155.         <tr>  
    156.             <td valign="top">Signature:   
    157.             </td>  
    158.             <td valign="top"><asp:TextBox ID="TextBoxSignature" runat="server" Height="50px" TextMode="MultiLine"  
    159.             Width="250px"></asp:TextBox>  
    160.             </td>  
    161.         </tr>  
    162.         <tr>  
    163.            
    164.             <td colspan="2">  
    165.                
    166.         <asp:Button ID="ButtonUpdate" runat="server" OnClick="ButtonUpdate_Click" Text="Save My Information" />  
    167.         <asp:Label ID="LabelPhotoError" runat="server" EnableViewState="false" Font-Bold="true" ForeColor="red"></asp:Label>  
    168.           
    169.             </td>  
    170.         </tr>  
    171.     </table>  
    172.       
    173.     <asp:Label ID="l1" runat="server"></asp:Label>&nbsp;&nbsp;   
    174.     <asp:Label ID="l2" runat="server"></asp:Label>  
    175. </asp:Content>  
     
    Regards,
    Terry
     
View Complete Thread