1

I am designing a page for an asp.net core web app that allows the user to design an object, and download the json for it upon a button press. In order to do this, the button uses jquery and ajax to hit the following controller method:

// GET: Missions/DownloadMission/5
[HttpGet, ActionName("DownloadMission")]
public FileContentResult DownloadMission(int id)
{
    Mission toDownload = db.Missions.Find(id);
    string json = JsonConvert.SerializeObject(toDownload);
    string fileName = (toDownload.Title + ".txt");
    string mimeType = "text/plain";
    byte[] bytes = Encoding.ASCII.GetBytes(json);
    return new FileContentResult(bytes, mimeType)
    {
        FileDownloadName = fileName
    };
}

The .done(function() { in the ajax request gets ran, and there are no errors. So, it is seemingly working as intended. However the file does not get downloaded.

I have tested this on multiple browsers (Chrome, Firefox, Edge, even IE), and turned off all security settings and blockers. None of these changes have effected anything, so I am stumped.

Serus
  • 144
  • 2
  • 11

1 Answers1

1

The code provided on the endpoint is correct, however there needs to be handling on the ajax/js side in order to actually download the file; as currently it is just returning and doing nothing with it.

Multiple very detailed references for which can be found here: Handle file download from ajax post

Serus
  • 144
  • 2
  • 11