locked
Uploading files in asp.net RRS feed

  • Question

  • User1794681497 posted

    Hi I created button upload and with popup window after I upload documents using browse in popup window and click ok its not displaying aspx page please suggest me how to do this...Below is the code

    <div id="popup1" class="overlay">

    <div class="popup">

    <h2>Upload Document</h2>

    <a class="close" href="#">×</a>

    <div class="content">

    <form action="" method="post" enctype="multipart/form-data" name="form" id="form1">

    <label>Choose File

     <asp:Fileupload ID="FileUpload1" class="multi" runat="server"></asp:Fileupload>

      <label> Destination Folder

    <asp:Fileupload ID="FileUpload2" class="multi" runat="server"></asp:Fileupload>

    <br />

    </label>

    </form>

    <asp:Button ID="btnOk" runat="server" Text="OK" />

    <asp:Button ID="btnCancel" runat="server" Text="Cancel" />

    </div>

    </div>

    </div>

    Wednesday, December 30, 2015 4:34 AM

Answers

  • User1724605321 posted

    Hi ritheesha,

    Uploading files in asp.net

    Code below is for your reference:

       <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="btnsave_Click"  Text="Save" style="width:85px" />
             <br /><br />
             <asp:Label ID="lblmessage" runat="server" />
          </div>
          
       </form>

    Code behind:

    protected void btnsave_Click(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();
       }
    }

    Please refer to below links for details :

    https://msdn.microsoft.com/en-us/library/ms227669.aspx

    http://www.tutorialspoint.com/asp.net/asp.net_file_uploading.htm

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 7:30 AM
  • User1724605321 posted

    Hi  ritheesha,

    Hi Nan Can I have code in vb.net because I am using vb.net

    Refer to below demo/code with vb code :

    https://msdn.microsoft.com/en-us/library/aa479405.aspx

    https://msdn.microsoft.com/en-us/library/ms227669.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 7:39 AM

All replies

  • User1724605321 posted

    Hi ritheesha,

    Uploading files in asp.net

    Code below is for your reference:

       <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="btnsave_Click"  Text="Save" style="width:85px" />
             <br /><br />
             <asp:Label ID="lblmessage" runat="server" />
          </div>
          
       </form>

    Code behind:

    protected void btnsave_Click(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();
       }
    }

    Please refer to below links for details :

    https://msdn.microsoft.com/en-us/library/ms227669.aspx

    http://www.tutorialspoint.com/asp.net/asp.net_file_uploading.htm

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 7:30 AM
  • User1794681497 posted

    Hi Nan Can I have code in vb.net because I am using vb.net

    Wednesday, December 30, 2015 7:31 AM
  • User1724605321 posted

    Hi  ritheesha,

    Hi Nan Can I have code in vb.net because I am using vb.net

    Refer to below demo/code with vb code :

    https://msdn.microsoft.com/en-us/library/aa479405.aspx

    https://msdn.microsoft.com/en-us/library/ms227669.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 7:39 AM