title: File Upload and Download using Asp.net
body: <p>I am trying to use file upload control to upload and then download file using Asp.net C#, But it is giving me a Directory not found exception. Can anybody help me with this, where I am making the mistake?</p>
  
  <p>Here is my .aspx file:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploadcheck.aspx.cs" Inherits="fileuploadcheck" %&gt;
  
      &lt;!DOCTYPE html&gt;
  
      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head runat="server"&gt;
          &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
            &lt;table style="padding: 20px;"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="lblFilename" runat="server" Text="Browse:"&gt;&lt;/asp:Label&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:FileUpload ID="fileUpload1" runat="server" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &amp;nbsp;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &amp;nbsp;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;asp:LinkButton ID="OnLnkUpload" runat="server" OnClick="OnLnkUpload_Click" Font-Underline="False"&gt;Upload&lt;/asp:LinkButton&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:LinkButton ID="OnLnkDownload" runat="server" OnClick="OnLnkDownload_Click" Font-Underline="False"&gt;Download&lt;/asp:LinkButton&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/div&gt;
          &lt;/form&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Here is my Code Behind File:</p>
  
  <pre><code>    using System;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Xml.Linq;
      using System.IO;
  
      public partial class fileuploadcheck : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
          /// To save/upload files in folder and download files from folder in asp.net
          /// &lt;/summary&gt;
          string filename = string.Empty;
  
          protected void btnUpload_Click(object sender, EventArgs e)
          {
  
          }
  
          protected void OnLnkUpload_Click(object sender, EventArgs e)
          {
              filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
              fileUpload1.SaveAs(Server.MapPath("Files/" + filename));
  
              Response.Write("File uploaded sucessfully.");
              lblFilename.Text = "Files/" + fileUpload1.FileName;
          }
  
          // To download uplaoded file
          protected void OnLnkDownload_Click(object sender, EventArgs e)
          {
              if (lblFilename.Text != string.Empty)
              {
                  if (lblFilename.Text.EndsWith(".txt"))
                  {
                      Response.ContentType = "application/txt";
                  }
                  else if (lblFilename.Text.EndsWith(".pdf"))
                  {
                      Response.ContentType = "application/pdf";
                  }
                  else if (lblFilename.Text.EndsWith(".docx"))
                  {
                      Response.ContentType = "application/docx";
                  }
                  else
                  {
                      Response.ContentType = "image/jpg";
                  }
  
                  string filePath = lblFilename.Text;
  
                  Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
                  Response.TransmitFile(Server.MapPath(filePath));
                  Response.End();
              }
          }
      }
  </code></pre>