View:
$ (function () {
$ ( "# Sub") the Click (function () {.
Var FD = the FormData new new ();
fd.append ( "filel", $ ( "# File") GET (0). .files [0]);
$ .ajax ({
URL: "@ Url.Content (" ~ / Home / ImportFile ")",
type: "POST",
contentType: false, // file type (to false, no use the default file type, any type)
the processData: false, // defaults to false, when set to true, when submitted jquery ajax Data not serialized, but direct use of Data
Data: FD,
Success: function (E) {
IF (E == ". 1") {
Alert ( "file uploaded successfully!");
}
the else {
Alert ( "failed files!");
}
}
});
});
});
Controller:
public ActionResult ImportFile(HttpPostedFileBase filel)
{
try
{
string path = Server.MapPath("~/App_Data") +"/"+ filel.FileName;
filel.SaveAs(path);
return Content("1");
}
catch (Exception)
{
return Content("0");
}
}