title: How to create a button that downloads a file in ASP.net MVC
body: <p>I have a button and a calendar in my html that when the user clicks the button it Downloads a file from a file path and if the user picks a particular date it will download that file. My problem was, instead of downloading the file in the webpage it displays the file in the web browser. How can I resolve this issue? Thank you.. I've also added the images at the bottom.</p>
  <pre><code>    public ActionResult Download(DateTime? downloaddate)
        {
            if (downloaddate == null)
          {
              string path = AppDomain.CurrentDomain.BaseDirectory + &quot;logs/&quot;;
              string filename2 = &quot;industrialcomm-payload-&quot; + DateTime.Now.ToString(&quot;yyyyMMdd&quot;) + &quot;.log&quot;;
              byte[] filebytes = System.IO.File.ReadAllBytes(path + filename2);
              string contentType = &quot;text/plain&quot;;
  
  
              return File(filebytes, contentType);
          }
          else
          {
              DateTime Date = new DateTime();
              Date = (DateTime)downloaddate;
  
              string path1 = AppDomain.CurrentDomain.BaseDirectory + &quot;logs/&quot;;
              string filename = &quot;industrialcomm-payload-&quot; + Date.ToString(&quot;yyyyMMdd&quot;) + &quot;.log&quot;;
              byte[] filebytes1 = System.IO.File.ReadAllBytes(path1 + filename);
              string contentType1 = &quot;text/plain&quot;;
  
              return File(filebytes1, contentType1);
          }
  
  
       @using (Html.BeginForm(&quot;Download&quot;, &quot;Home&quot;, FormMethod.Post))
        {
           &lt;input type=&quot;date&quot; id=&quot;myDate&quot; data-date-format=&quot;YYYY DD MMMM&quot; value=&quot;&quot;name=&quot;downloaddate&quot; style=&quot;border:medium; &quot;&gt;
  
      &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot; style=&quot;font-size:larger; font-weight:bolder;&quot;&gt;Download Log&lt;/button&gt; 
  
  
  
        }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/9cao6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9cao6.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/5h3IO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5h3IO.png" alt="enter image description here" /></a></p>