By using MVC4 ,I want to Download the file ,But i am not getting .
This is my Code using JSONResult
This is My Index Click Function .
function downLoadFile(rowIndex) {
var ddl = document.getElementById('ddlYear');
var ddlSectedYear = $('#ddlYear')[0][ddl.selectedIndex].value;
alert('row ' + rowIndex + ' download button clicked');
$.ajax({
url: '@Url.Action("Download", "Form_16")',
type: "POST",
dataType: "json",
data: JSON.stringify({id:rowIndex, ddlYear: ddlSectedYear }),
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("File Success For Download");
},
error: function () {
// $('label[for=lblError]').html('An error Occured while performing an Operation');
alert("Files download Failed");
}
});
return false;
}
and in Controller i am passing like this.
public ActionResult Download(string id, string ddlYear)
{
int fid = Convert.ToInt32(id);
var files = GetFiles(ddlYear);
string DocName=string.Empty;
foreach (var item in files)
{
if (fid == item.FileId)
DocName = item.FileName;
}
var path = Server.MapPath(@"~/Form-16/" + "/" + ddlYear + "/" + DocName);
DownloadDetails(DocName, ddlYear);
return Json( JsonRequestBehavior.AllowGet);
}
public FileResult DownloadDetails(string DocName, string ddlYear)
{
var path = Server.MapPath(@"~/Form-16/" + "/" + ddlYear + "/" + DocName);
string file = Path.Combine(path, DocName);
return File(file, System.Net.Mime.MediaTypeNames.Application.Octet, DocName);
}
Please help me anyone .
Thanks & Regards
Manorama.
ManoRama