title: File Upload in asp.net C#- Null reference error
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 Null reference exception(Object reference not set to an instance of an object). Can anybody help me with this, where I am making the mistake?</p>
  
  <p>Here is my aspx code:</p>
  
  <pre><code>&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;
  </code></pre>
  
  <p>Here is my code behind file: </p>
  
  <pre><code>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>