Uploading a File in ASP.Net Web API

In this article I am doing to show an example of uploading a file in ASP.NET Web API.

Introduction

This article showsan example of uploading a file in the ASP.NET Web API. Uploading the file from the client is a basic operation. The file can be upload to the web server. By default, the process of file uploading is asynchronous. The developers of ASP.NET use the HTML file input field.

Now I illustrate the process of uploading a file to the web server.

Step 1

Create a MVC4 Web API application "FileUpload".

  • Start Visual Studio 2010 and select "New Project" from the Start Page.
  • In the Template window, select "Installed template" -> "Visual C#" -> "Web".
  • Choose "ASP. NET MVC 4 Web Application" then change its name.
  • Click on the "OK" button.

file.jpg

  • MVC 4 Project window:

file1.jpg

  • Select "Web API" and click on the "OK" button.

Step 2

Change the name of "ValuesController" to "DocFileController.cs".

  • In the Solution Explorer.
  • Select "Controller Folder" -> "ValuesController.cs".
  • Rename the "ValuesController" to "DocFileController".

Write this code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using System.Web;  
  8. using System.IO;  
  9. namespace FileUpload.Controllers  
  10. {  
  11.     public class DocFileController : ApiController  
  12.     {  
  13.         public HttpResponseMessage Post()  
  14.         {  
  15.             HttpResponseMessage result = null;  
  16.             var httpRequest = HttpContext.Current.Request;  
  17.            if (httpRequest.Files.Count > 0)  
  18.             {  
  19.                 var docfiles = new List<string>();  
  20.                 foreach (string file in httpRequest.Files)  
  21.                 {  
  22.                     var postedFile = httpRequest.Files[file];  
  23.                     var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);  
  24.                     postedFile.SaveAs(filePath);  
  25.                     docfiles.Add(filePath);  
  26.                 }  
  27.                 result = Request.CreateResponse(HttpStatusCode.Created, docfiles);  
  28.             }  
  29.             else  
  30.             {  
  31.                 result = Request.CreateResponse(HttpStatusCode.BadRequest);  
  32.             }  
  33.              return result;  
  34.         }  
  35.     }  
  36. }  
The code above uses the "POST" method. This method looks for the  request object. If there is any posted file then it will generate on the server and it returns the full path of the file. If there is no posted file then it returns the 401 status and bad request.

 

Step 3

Open the "index.cshtml" file then:

  • In the "Solution Explorer".

  • Select "Views folder" -> "Home folder" -> "index.cshtml".

file2.jpg

And write this code in this file:

  1. <header>  
  2.     <div class="content-wrapper">  
  3.         <div class="float-left">  
  4.             <p class="site-title">  
  5.                 <a href="~/">ASP.NET Web API File Upload</a></p>  
  6.         </div>  
  7.     </div>  
  8. </header>  
  9. <div id="body">  
  10.     <form name="form1" method="post" action="api/docfile" enctype="multipart/form-data">  
  11.     <div>  
  12.         <label>  
  13.            Brows File</label>  
  14.         <input name="myFile" type="file" />  
  15.     </div>  
  16.     <div>  
  17.         <input type="submit" value="Upload" />  
  18.     </div>  
  19.     </form>  
Step 4

Now execute the application; press F5.

fole3.jpg

Click on the "Browse" button.

file4.jpg

Select one image and click on the "Open" button. It now shows the path of the file.

file5.jpg

Now click on the "Upload" button.

file6.jpg

Now it shows the path where the selected file is saved.