Skip to content Skip to sidebar Skip to footer

How To Send Uploaded File From Javascript To Controller In Mvc?

In my MVC, i have a view and that contains one file upload control and one button. Copy

View

@using (@Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 <tablestyle="border: solid thin; margin: 10px 10px 10px 10px"><trstyle="margin-top: 10px"><td>
             @Html.Label("Select a File to Upload")
             <br /><br /><inputtype="file"name="myfile"><inputtype="submit"value="Upload" /></td></tr></table>
}

Solution 2:

you cannot send file content via javascript (unless HTMl5). and you are doing totally wrong. if you want to do HTML5 based solution via FileReader api then you need to check this out. FileReader Api

Just put a form tag and use the same name of the input in the controller action to perform model binding

@using(Html.BeginForm("yourAction","YourControl",FormMethod.Post))
{
 <inputtype="file"id="fileUpload" />
}

then in controller.

[HTTPPost]
public ActionResult Final(HttpPostedFileBase fileUpload)
  {
     //here i have got the files value is null.
  }

Solution 3:

Below code will do a full post back in an hidden form which will give an illusion of ajax file upload. Try it:

Update:

JS

functionUpload(sender) {
    var iframe = $("<iframe>").hide();
    var newForm = $("<FORM>");
    newForm.attr({ method: "POST", enctype: "multipart/form-data", action: "/ControllerName/Final" });        
    var $this = $(sender), $clone = $this.clone();
    $this.after($clone).appendTo($(newForm));
    iframe.appendTo($("html")).contents().find('body').html($(newForm));
    newForm.submit();
}

HTML

<inputtype="file"id="Uploadfile" name="Uploadfile" />
<inputtype="button" onclick="Upload($('#UploadFile'));"/>

Controller

public ActionResult Final(HttpPostedFileBase Uploadfile){
     //here you can use uploaded file
}

Solution 4:

As a completion from Ravi's answer, I would suggest to use the following using statement:

@using(Html.BeginForm("yourAction","YourControl",FormMethod.Post, new { enctype="multipart/form-data" }))
{
   <inputtype="file"id="fileUpload" />
}

Solution 5:

You can do it by using json data to view.

As instance,

Controller

public ActionResult Products(string categoryid)
{
List<catProducts> lst = bindProducts(categoryid);
return View(lst);
}   
public JsonResult Productsview(string categoryid)
{
//write your logicvar Data = new { ok = true, catid = categoryid};
return  Json(Data, JsonRequestBehavior.AllowGet);
}

View:

@{
ViewBag.Title = "Index";
}

@model ASP.NETMVC.Controllers.Categories

<h2>List Of Categories</h2>

@Html.ListBox("lst_categories", (IEnumerable<SelectListItem>)  ViewBag.Categories)


<scripttype="text/javascript">
$(function () {

$('#lst_categories').change(function () {

var catid = $('#lst_categories :selected').val();

$.ajax({
url: '@Url.Action("Productsview", "Jquery")',
type: 'GET',
dataType: 'json',
data: { categoryid: catid },
cache: false,

success: function (Data) {
if (Data.ok) {

var link = "@Url.Action("Products", "Jquery", new { categoryid = "catid" })";
link = link.replace("catid", Data.catid);
alert(link);
window.location.href = link;
}
}
});
});
});
</script>

Post a Comment for "How To Send Uploaded File From Javascript To Controller In Mvc?"