0

I'm sending 3 items to my controller, "From","To","StudentID" ..

I get the in LinQ and I want to pass this result to browser as a CSV file to download it, but I don't know what to write in my view to get this result,

Here is my controller:

public FileContentResult DownloadFile(String from,String to,int  StudentID)
{
    var myResult = (from t in db.students
                    where( t.date>=from && t.date<=to ) && t.studentID==StudentID
                    select new student {
                      stName = t.name,
                      st.fname = t.name
                    }).toList();
    var st = DataTag.NoExlusion;

    var csvByte = ASCIIEncoding
                    .ACII
                    .GetBytes( CSVexport.getCsv(myResult.toList(),st );

    return this.File(csvByte,"text/csv",".csv");
}

My View

            var data = { "StudentID": id, "from": frm_, "to": to_ };
   $.ajax({

                                    dataType: "json",
                                    type: "POST",
                                    url: "@Url.Action("DownloadFile", "Students")",
                                     contentType: "application/json;charset=utf-8",



                                        data: JSON.stringify(data),
                                          success: function (result) {


                        }
                        })
  • What's wrong with what you have? Looks reasonable to me other than you aren't specifying a proper file name for the downloaded file. – James Dec 13 '17 at 20:54
  • @James i dont get anything in browser to download,i dont know what to write in my view to get this result –  Dec 13 '17 at 21:13
  • does the request complete? No errors? What do you see in the HTTP response? – James Dec 13 '17 at 21:14
  • @James i get no error,my problem is ,first i cant use httpGet,second i dont know what to write in view –  Dec 13 '17 at 21:32
  • You just create a link in the view that passes the 3 values to that method. –  Dec 13 '17 at 21:55
  • @moris1983 ah I see, yeah an action link with the relevant parameters would do the trick. Out of curiosity, why can't you use GET? – James Dec 13 '17 at 21:58
  • @StephenMuecke Im passing those 3 values via ajax post,my problem is how to get the result of this controller –  Dec 14 '17 at 07:23
  • 1
    You cannot return a file using ajax! –  Dec 14 '17 at 07:25
  • @James I cant get httpGet because im passing 3 3 values with ajax post,so if I use httpget,this 3 values never reach my controller, –  Dec 14 '17 at 07:25
  • @StephenMuecke yes but can I pass values by ajax and get them by some other trick?can you tell me please? –  Dec 14 '17 at 07:26
  • You need to redirect to that method - Again, you CANNOT return a file using ajax (and you have not shown any of your relevant code) –  Dec 14 '17 at 07:28
  • @StephenMuecke im going to update my code here with the simple view,then kindly take a look –  Dec 14 '17 at 07:30
  • Y9ou have not given any indication how of what calls that ajax call. Just do a redirect. e.g. using `location.href = '@Url.Action("DownloadFile", "Students")' + '?StudentID=' + id + .......` –  Dec 14 '17 at 07:42
  • Just to reiterate what Stephen has said, this won't work via AJAX because you don't have permissions to write to the file system with JavaScript, saving files has to be done by the browser. Generally a redirecting to another page to trigger the download has the same affect. – James Dec 14 '17 at 09:26
  • @James it worked,great!!! thanks a million –  Dec 14 '17 at 09:34

0 Answers0