Re: How to upload files in Button Click eventUpload Files

  •  06-09-2009, 4:10 AM

    Re: How to upload files in Button Click eventUpload Files

    Hi,
     
    Please check this sample code :
     
    1. <%@ Page Language="C#" Title="First sample" %>  
    2.   
    3. <%@ Import Namespace="CuteWebUI" %>  
    4. <%@ Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %>  
    5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">  
    6.   
    7. <script runat="server">  
    8.     protected void Button1_Click(object sender, EventArgs e)   
    9.     {   
    10.         System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();   
    11.         msg.From = "mymail@myhost.com";   
    12.         msg.To = "yourmail@yourmail.com";   
    13.         msg.Subject = tbTitle.Text;   
    14.         msg.Body = tbMessage.Text;   
    15.            
    16.         foreach (AttachmentItem item in UploadAttachments1.Items)   
    17.         {   
    18.             if (!item.Checked) continue;   
    19.   
    20.             //string filename = item.FileName;   
    21.             //int filesize=item.FileSize;   
    22.             //Stream stream=item.OpenStream();   
    23.   
    24.             System.Web.Mail.MailAttachment ma  
    25.                 = new System.Web.Mail.MailAttachment(item.GetTempFilePath());   
    26.             msg.Attachments.Add(ma);   
    27.         }   
    28.         //System.Web.Mail.SmtpMail.Send(msg);   
    29.   
    30.         //remove all items    
    31.         //UploadAttachments1.DeleteAllAttachments();   
    32.   
    33.            
    34.     }   
    35.   
    36. </script>  
    37.   
    38. <html xmlns="http://www.w3.org/1999/xhtml">  
    39. <head id="Head1" runat="server">  
    40. </head>  
    41. <body>  
    42.     <form id="Form1" runat="server">  
    43.         <table>  
    44.             <tr>  
    45.                 <td>  
    46.                 </td>  
    47.                 <td>  
    48.                     Send a mail:   
    49.                 </td>  
    50.             </tr>  
    51.             <tr>  
    52.                 <td>  
    53.                     Title :   
    54.                 </td>  
    55.                 <td>  
    56.                     <asp:TextBox runat="server" ID="tbTitle"></asp:TextBox>  
    57.                 </td>  
    58.             </tr>  
    59.             <tr>  
    60.                 <td>  
    61.                 </td>  
    62.                 <td>  
    63.                     <CuteWebUI:UploadAttachments runat="server" ID="UploadAttachments1" InsertText="Select attachments">  
    64.                         <ItemCellStyle CssClass="Cell" />  
    65.                         <TableStyle CssClass="Table" />  
    66.                     </CuteWebUI:UploadAttachments>  
    67.                 </td>  
    68.             </tr>  
    69.             <tr>  
    70.                 <td>  
    71.                     Message :   
    72.                 </td>  
    73.                 <td>  
    74.                     <asp:TextBox runat="server" ID="tbMessage" Rows="8" TextMode="MultiLine" Width="400px"></asp:TextBox>  
    75.                 </td>  
    76.             </tr>  
    77.             <tr>  
    78.                 <td>  
    79.                 </td>  
    80.                 <td>  
    81.                     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" Width="107px" />  
    82.                 </td>  
    83.             </tr>  
    84.         </table>  
    85.     </form>  
    86. </body>  
    87. </html>  
     
    Regards,
    Terry
View Complete Thread