asp.net FileUpload example: how to rename file when upload.

  1. <%@ Page Language="C#" %>  
  2. <%@ Import Namespace="System.IO" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5.   
  6. <script runat="server">  
  7.     protected void Button1_Click(object sender, System.EventArgs e) {  
  8.         string uploadFolder = Request.PhysicalApplicationPath + "UploadFile\\";  
  9.         if (FileUpload1.HasFile)  
  10.         {  
  11.             string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);  
  12.             FileUpload1.SaveAs(uploadFolder + "Test"+ extension);  
  13.             Label1.Text = "File uploaded successfully as: " + "Test"+ extension;  
  14.         }  
  15.         else  
  16.         {  
  17.             Label1.Text = "First select a file.";  
  18.         }  
  19.     }  
  20. </script>  
  21.   
  22. <html xmlns="http://www.w3.org/1999/xhtml">  
  23. <head id="Head1" runat="server">  
  24.     <title>asp.net FileUpload example: how to rename file when upload (change file name when upload)</title>  
  25. </head>  
  26. <body>  
  27.     <form id="form1" runat="server">  
  28.     <div>  
  29.         <h2 style="color:Green">asp.net FileUpload example: File Rename</h2>  
  30.         <asp:Label   
  31.              ID="Label1"   
  32.              runat="server"   
  33.              Font-Size="Large"  
  34.              ForeColor="OrangeRed"  
  35.              >  
  36.         </asp:Label>  
  37.         <br /><br />  
  38.         <asp:FileUpload   
  39.              ID="FileUpload1"   
  40.              runat="server"   
  41.              BackColor="DeepPink"   
  42.              ForeColor="AliceBlue"   
  43.              />  
  44.         <asp:Button   
  45.              ID="Button1"   
  46.              runat="server"   
  47.              Font-Bold="true"   
  48.              ForeColor="DeepPink"   
  49.              OnClick="Button1_Click"  
  50.              Text="Upload It"  
  51.              />     
  52.     </div>  
  53.     </form>  
  54. </body>  
  55. </html>


How to rename file when upload in asp.net FileUpload

asp.net FileUpload example: how to rename file when upload (change file name when upload)
FileUploadRename.aspx
  1. <%@ Page Language="C#" %>  
  2. <%@ Import Namespace="System.IO" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5.   
  6. <script runat="server">  
  7.     protected void Button1_Click(object sender, System.EventArgs e) {  
  8.         string uploadFolder = Request.PhysicalApplicationPath + "UploadFile\\";  
  9.         if (FileUpload1.HasFile)  
  10.         {  
  11.             string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);  
  12.             FileUpload1.SaveAs(uploadFolder + "Test"+ extension);  
  13.             Label1.Text = "File uploaded successfully as: " + "Test"+ extension;  
  14.         }  
  15.         else  
  16.         {  
  17.             Label1.Text = "First select a file.";  
  18.         }  
  19.     }  
  20. </script>  
  21.   
  22. <html xmlns="http://www.w3.org/1999/xhtml">  
  23. <head id="Head1" runat="server">  
  24.     <title>asp.net FileUpload example: how to rename file when upload (change file name when upload)</title>  
  25. </head>  
  26. <body>  
  27.     <form id="form1" runat="server">  
  28.     <div>  
  29.         <h2 style="color:Green">asp.net FileUpload example: File Rename</h2>  
  30.         <asp:Label   
  31.              ID="Label1"   
  32.              runat="server"   
  33.              Font-Size="Large"  
  34.              ForeColor="OrangeRed"  
  35.              >  
  36.         </asp:Label>  
  37.         <br /><br />  
  38.         <asp:FileUpload   
  39.              ID="FileUpload1"   
  40.              runat="server"   
  41.              BackColor="DeepPink"   
  42.              ForeColor="AliceBlue"   
  43.              />  
  44.         <asp:Button   
  45.              ID="Button1"   
  46.              runat="server"   
  47.              Font-Bold="true"   
  48.              ForeColor="DeepPink"   
  49.              OnClick="Button1_Click"  
  50.              Text="Upload It"  
  51.              />     
  52.     </div>  
  53.     </form>  
  54. </body>  
  55. </html>  








0 comments:

Post a Comment

 
Top