---
title: "File Upload for any specific file in MVC"  
description: "In this blog I'm trying to exploring the concept of File Upload for any specify files such as PDF.  File upload is very useful functionality in web ap"  
author: "Vijay Shukla"  
published: 2013-09-25  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/587/file-upload-for-any-specific-file-in-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# File Upload for any specific file in MVC

In this blog I'm trying to [exploring](https://yourviews.mindstick.com/view/85453/trends-of-the-space-race-and-the-moon-landing-exploring-new-frontiers) the concept of [File Upload](https://www.mindstick.com/articles/13047/file-upload-and-download-in-mvc) for any specify files such as PDF.\

File upload is very useful [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) in [web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about). Here is a simple [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) of file upload in [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) MVC4.

Create a View for file upload:

Add a view (Index) and create a file [upload control](https://www.mindstick.com/forum/109/why-do-uploads-fail-while-using-an-asp-dot-net-file-upload-control-to-upload-large-files) with the help of HTML as shown below:

```
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "frmUploadFile", @enctype = "multipart/form-data" }))
{    <input type="text" id="txtFileName" name="txtFileName" />    <input type="file" id="fileUpload" name="fileUpload" accept="application/pdf" style="width: 90px;" onchange="validation()" /><br />
    <input type="submit" />
    <div style="color:red; font-size:larger;">@ViewBag.Message</div>
}
```

##### Create a JavaScript for Validate file is PDF or not

```
<script type="text/javascript">    function validation() {        var fname = (document.getElementById('fileUpload').value).substring(12);
        var re = /(\.pdf)$/i;        if (re.exec(fname)) {            document.getElementById('txtFileName').value = (document.getElementById('fileUpload').value).substring(12);
        }
        else            alert("file not supported!");    }</script>
```

\

After adding view and [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) the following code in the [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) to upload the file on the server:

```
public ActionResult Index(){     ViewBag.Message = "";     return View();}[HttpPost]public ActionResult Index(HttpPostedFileBase fileUpload){     var fileName = Path.GetFileName(fileUpload.FileName);     if (fileUpload != null)     {         var newFileName = Guid.NewGuid();         fileUpload.SaveAs(Server.MapPath("~\\files\\" + newFileName+".pdf"));
         ViewBag.Message = "File Upload!";     }     return View();}
```

\

Run this application, you will see the following screen:

\

![File Upload for any specific file in MVC](https://www.mindstick.com/blogs/3bffd618-900f-4922-846e-5020a84d1a4e/images/79b5be92-0af4-48f0-952e-151f7aa1a988.png)

---

Original Source: https://www.mindstick.com/blog/587/file-upload-for-any-specific-file-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
