Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • File Upload in .NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 184
    Comment on it

    The FileUpload controls the user to browse the file from any location and upload it where user wants. Once, the user enters the filename in the textbox the file will gets saved on the hard disk of the user. See the code example below:

    <body>
       <form id="form1" runat="server">
    
          <div>
             <h3> File Upload:</h3>
             <br />
             <asp:FileUpload ID="FileUpload1" runat="server" />
             <br /><br />
             <asp:Button ID="btnsave" runat="server" onclick="btnsaveClick"  Text="Save" style="width:85px" />
             <br /><br />
             <asp:Label ID="lblmessage" runat="server" />
          </div>
    
       </form>
    </body>
    

     

    After this you need to write the code on code behind file:

    protected void btnsaveClick(object sender, EventArgs e)
    {
       StringBuilder sb = new StringBuilder();
    
       if (FileUpload1.HasFile)
       {
          try
          {
             sb.AppendFormat(" Uploading file: {0}", FileUpload1.FileName);
    
             //saving the file
             FileUpload1.SaveAs("<c:\\SaveDirectory>" + FileUpload1.FileName);
    
             //Showing the file information
             sb.AppendFormat("<br/> Save As: {0}",  FileUpload1.PostedFile.FileName);
             sb.AppendFormat("<br/> File type: {0}",    FileUpload1.PostedFile.ContentType);
             sb.AppendFormat("<br/> File length: {0}",  FileUpload1.PostedFile.ContentLength);
             sb.AppendFormat("<br/> File name: {0}",  FileUpload1.PostedFile.FileName);
    
          }catch (Exception ex)
          {
             sb.Append("<br/> Error <br/>");
             sb.AppendFormat("Unable to save file <br/> {0}", ex.Message);
          }
       }
       else
       {
          lblmessage.Text = sb.ToString();
       }
    }
    
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: