I am not Download the File In MVC4

Posted by Manorama under ASP.NET MVC on 8/26/2013 | Points: 10 | Views : 3482 | Status : [Member] | Replies : 1
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


Responses

Posted by: Nitesh.Luharuka on: 9/2/2013 [Member] Starter | Points: 25

Up
0
Down
Check this link -

http://rachelappel.com/upload-and-download-files-using-asp.net-mvc#

Nitesh Luharuka
http://www.niteshluharuka.com

Manorama, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response