title: How to do File Upload Without Page Refresh or redirect in ASP.NET MVC
body: <p>I am uploading a file using via a view linked to a controller however after the upload is uploaded the application is either trying to refresh or redirect and I need to prevent this. May you please point me in the right direction to avoid redirection and refresh? I have done a bit of reading and I suspect that this line <code>action="/api/BulkUpload"&gt;</code>might be causing the problem.</p>
  
  <p>My Controller </p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.IO;
  using System.Net;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using System.Text;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Http;
  using Repositories.BulkUpload;
  using Repositories.Interfaces.BulkUpload;
  
  namespace SimSentinel.Controllers
  {
      //[Authorize]
     public class BulkUploadController : ApiController
     {
        private readonly IBulkUploadRepository _bulkUploadRepository;
        public async Task&lt;HttpResponseMessage&gt; PostFile()
        {
           // Check if the request contains multipart/form-data.
           if (!Request.Content.IsMimeMultipartContent())
           {
              throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
           }
  
           string root = HttpContext.Current.Server.MapPath("~/Files");
           var provider = new FormDataStreamer(root);
  
           try
           {
              StringBuilder sb = new StringBuilder(); // Holds the response body 
  
  
  
              // Read the form data and return an async task. 
              await Request.Content.ReadAsMultipartAsync(provider);
  
              // This illustrates how to get the form data. 
              foreach (var key in provider.FormData.AllKeys)
              {
                 foreach (var val in provider.FormData.GetValues(key))
                 {
                    sb.Append(string.Format("{0}: {1}\n", key, val));
                 }
              }
  
  
              // This illustrates how to get the file names for uploaded files. 
              foreach (var file in provider.FileData)
              {
                 FileInfo fileInfo = new FileInfo(file.LocalFileName);
                 sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length));
              }
              return new HttpResponseMessage()
              {
                 Content = new StringContent(sb.ToString())
              };
           }
           catch (System.Exception e)
           {
              return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
           }
  
        }
  
        public class FormDataStreamer : MultipartFormDataStreamProvider
        {
           public FormDataStreamer(string rootPath) : base(rootPath) { }
           public FormDataStreamer(string rootPath, int bufferSize) : base(rootPath, bufferSize) { }
           public override string GetLocalFileName(HttpContentHeaders headers)
           {
              var srcFileName = headers.ContentDisposition.FileName.Replace("\"", "");
              return Guid.NewGuid() + Path.GetExtension(srcFileName);
           }
        }
  
  
  
  
  
     }
  }
  </code></pre>
  
  <p><strong>MY HTML</strong></p>
  
  <pre><code> &lt;form name="form1" method="post" enctype="multipart/form-data" action="/api/BulkUpload"&gt;
           &lt;div&gt;
              &lt;label for="caption"&gt;Image Caption&lt;/label&gt;
              &lt;input name="caption" type="text" /&gt;
           &lt;/div&gt;
           &lt;div&gt;
              &lt;label for="image1"&gt;Image File&lt;/label&gt;
              &lt;input name="image1" type="file" /&gt;
           &lt;/div&gt;
           &lt;div&gt;
              &lt;input type="submit" value="ok" /&gt;
           &lt;/div&gt;
        &lt;/form&gt;
  </code></pre>