0

I have been dealing with a problem for hours,kindly help me,the following is my ajax which post data to controller :

$.ajax({
  dataType: "json",
  type: "POST",
  url: "@Url.Action("CreateCSVFile ","Turbine ")",
  contentType: "application/json;charset=utf-8",
  data: JSON.stringify(data),
  success: function(result) {}
})

It posts the result I want to controller,but problem starts from here that in my controller after making the export file,i don't see any thing in the browser to save it,i have no idea where is going wrong,the following is my controller:

public FileContentResult CreateCSVFile(string turbinename, string frm_date, string to_date)
    {
        var eventResult = (from c in DB.Events
                           where (c.m_turbine_id == turbineid.turbineID) && (c.m_time_stamp >= frmDate && c.m_time_stamp <= toDate)

                           select new EventLogPartialViewModel
                           {
                               Timestamp = c.m_time_stamp,
                               Description = c.m_event_log_description,
                               WindSpeed = c.m_wind_speed,
                               RPM = c.m_rpm,
                               Power = c.m_power
                           }).ToList().Select(x => new
                           {
                               Timestamp = x.Timestamp.ToString("dd/MM/yyyy H:mm:ss"),
                               Description = x.Description,
                               WindSpeed = x.WindSpeed,
                               RPM = x.RPM,
                               Power = x.Power
                           }).ToList().OrderByDescending(m => m.Timestamp);

        var st = DataTag.NoExclusion;
        string csv = "Charlie, Chaplin, Chuckles";
        byte[] csvBytes = ASCIIEncoding.ASCII.GetBytes(CSVExport.GetCsv(eventResult.ToList(), st));           
        return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "Report123.csv");
    }
moris62
  • 983
  • 1
  • 14
  • 41
  • nott sure if it comes from there, but you are exporting as ASCII, and you expect to read the result as UTF-8 for saving as file. That's kinda strange, and will probably bites you as soon as you have non-ascii characters in your result. – Pac0 Dec 12 '17 at 16:02
  • @pac0 whats the solution? – moris62 Dec 12 '17 at 17:08
  • nevermind, I just noticed that your two last lines don't use the same data at all. – Pac0 Dec 13 '17 at 06:39
  • @Biff MaGriff take a look – moris62 Dec 13 '17 at 09:24
  • related https://stackoverflow.com/questions/4668906/export-to-csv-using-mvc-c-sharp-and-jquery?rq=1 – Pac0 Dec 13 '17 at 09:26
  • @Pac0 its so strange,i have the data,but I cant post it as CSV,i took many approaches im getting crazy really – moris62 Dec 13 '17 at 09:36
  • can you try to do it without jQuery, by using a `` ? – Pac0 Dec 13 '17 at 09:41
  • also, in your setup, you are explicitly asking to do nothing with the result of the call : `success: function(result) {}` – Pac0 Dec 13 '17 at 09:42
  • for the ajax part, you can have a look at https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post – Pac0 Dec 13 '17 at 09:48
  • @Pac0 should I remove success: function(result) {}? – moris62 Dec 13 '17 at 10:49
  • you should have a look at this answer : https://stackoverflow.com/a/23797348/479251 – Pac0 Dec 13 '17 at 11:15

1 Answers1

0

To fit your particular function, you can do :

public FileContentResult CreateCSVFile()
{
    string csv = "Charlie, Chaplin, Chuckles";
    byte[] csvBytes = Encoding.UTF8.GetBytes(csv); // or get your bytes the way you want

    string contentType = "text/csv";
    var result = new FileContentResult(csvBytes, contentType);

    return result;
}

This is how you can create a FileContentResult.

However, I prefer to use this to send a response with a file :

[HttpGet]
public HttpResponseMessage GetYourFile()
{
    string csv = "Charlie, Chaplin, Chuckles";
    byte[] csvBytes = Encoding.UTF8.GetBytes(csv); // or get your bytes the way you want

    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(csvBytes);
    };
    result.Content.Headers.ContentDisposition =
        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
    {
        FileName = "YourFileName.csv"
    };
    result.Content.Headers.ContentType =
        new MediaTypeHeaderValue("text/csv");

    return result;
}

(adapted from How to return a file (FileContentResult) in ASP.NET WebAPI).

Extracting the "csv" part in bytes is another problem completely, I hope your problem was about the "create a file" part.

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • for the first solution you kindly corrected my code,i have to say its still not working,i get nothing in the browser to save,everything seems to be correct but the browser does not prompt "save as " – moris62 Dec 13 '17 at 07:59