1

I am new to mvc4 . I used the below code to download file from server to client in the controller:

public ActionResult IndexSpecification(int option_id) { int cat = (int)Session["category_Name"]; int prod = (int)Session["product_ID"]; int user = (int)Session["logged_in"]; string twoinone = cat + "_" + prod; f1 = Download(twoinone); return f1; } 

where Download function is:

public FileResult Download( string twoinone) { var webClient = new WebClient(); byte[] fileBytes = System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Download"), "a.rar")); string fileName = "Tellersoft.rar"; return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName); } 

The call to the controller is from ajax :

$.ajax({ type: "POST", url: base_url + '/Options/IndexSpecification', data: { option_id : 2 }, //dataType: 'json', encode: true, async: false, cache: false, success: function (data, status, jqXHR) { console.log(data); }, error: function (jqXHR, textStatus, errorThrown) { if (typeof (console) != 'undefined') { alert("oooppss"); } else { alert("something went wrong"); } } }); 

But downloading not working .not even returning any error. Please help

0

1 Answer 1

0
$.ajax({ type: "POST", url: base_url + '/Options/IndexSpecification', data: { option_id : 2 }, //dataType: 'json', encode: true, async: false, cache: false, success: function (data, status, jqXHR) { console.log(data); }, error: function (jqXHR, textStatus, errorThrown) { if (typeof (console) != 'undefined') { alert("oooppss"); } else { alert("something went wrong"); } } }); 

In your ajax request in success block you have to write this code:

$.ajax({ type: "POST", url:'/Home/Download', data: { option_id: 2 }, //dataType: 'json', encode: true, async: false, cache: false, success: function (data, status, jqXHR) { window.location = '/Home/Download?option_id=2'; }, error: function (jqXHR, textStatus, errorThrown) { if (typeof (console) != 'undefined') { alert("oooppss"); } else { alert("something went wrong"); } } }); 

you have to set your option id value globally and declare it in success section...

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

please try to format your code properly...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.