---
title: "Upload file using Ajax call using ASP.NET MVC"  
description: "Upload file using Ajax call using ASP.NET MVC"  
author: "Anonymous User"  
published: 2014-03-06  
updated: 2014-03-06  
canonical: https://www.mindstick.com/forum/1975/upload-file-using-ajax-call-using-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# Upload file using Ajax call using ASP.NET MVC

I would like to [upload](https://www.mindstick.com/forum/12860/how-to-upload-file-asynchronously-using-generic-handler) a file in my page using:

<input type="file" name="[FileName](https://www.mindstick.com/interview/23463/difference-between-include-filename-and-include-filename)">

I have a [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net), clicking on which an ajax post is done.

$.ajax({

[cache](https://www.mindstick.com/blog/222/clearing-cache-in-asp-dot-net): false,

async: true,

type: "POST",

url: '@Url.Content("~/OCR/OCRProcessor")',

data: '',

[success](https://www.mindstick.com/articles/12957/best-tips-to-make-your-ghostwriting-experience-a-success): [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) (data) {

$('#ocrresult').val(data);

}

});

I would like to get the file uploaded in the [controller action](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) [method](https://www.mindstick.com/forum/166/webservice-method) something like below:

HttpPostedFileBase hpf = Request.Files["FileName"] as HttpPostedFileBase

Please let me know the optimal way to achieve this task.

## Replies

### Reply by Pravesh Singh

Hi Ankita,\

$('#myFormId').submit(function() {

// submit the form

$(this).ajaxSubmit();

// return false to prevent normal browser submit and page navigation

return false;

});

Demo

This would be a No plugin approach (only in Html5), but I'm still recommending the plugin

$("#myFormId").submit(function(){

var formData = new FormData($(this)[0]);

$.ajax({

url: "YourPath/ToAction",

type: 'POST',

data: formData,

async: false,

success: function (data) {

alert(data)

},

cache: false,

contentType: false,

processData: false

});

return false;

});


---

Original Source: https://www.mindstick.com/forum/1975/upload-file-using-ajax-call-using-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
