0

I am generating a small Json file which i wan't the user to download. So i want the browser to prompt the user to download the file.

I've tried many of answers suggested in related questions but those won't work for me.

The request is made by a click on an actionlink:

@Ajax.ActionLink("Generate JSON", "GenerateOcJson", new AjaxOptions { HttpMethod = "POST" })

i've tried:

var cd = new System.Net.Mime.ContentDisposition { FileName = fileName, Inline = false };
Response.AppendHeader("Content-Disposition", cd.ToString());

return File(Encoding.UTF8.GetBytes(jsonString),
            "application/json",
            string.Format(fileName));

and:

Response.Clear();
Response.ContentType = "application/json";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.json");
Response.Write(jsonString);
Response.End();

But the browser won't download the file. I'm using MVC3 and this method is called by an actionlink. I've tried POST and GET requests.

If I inspect the request with Chrome i see the correct json had been written to the browser response.

Any clues? Thnx in advance

middelpat
  • 2,485
  • 1
  • 16
  • 29
  • Why have you set the content type to 'application/pdf' if it's JSON? Shouldn't it be 'application/json' or 'text/javascript' (there are several possibilities, see [http://stackoverflow.com/questions/477816/the-right-json-content-type](http://stackoverflow.com/questions/477816/the-right-json-content-type)) – MadSkunk Nov 27 '12 at 12:24
  • You are right, thats an copy error after trying lots and lots of solutions. I am aware of the contenttype for json. Thx for spotting this mistake. But even when fixing this the code doesn't work well – middelpat Nov 27 '12 at 12:27
  • "the browser won't download the file." — Tell us what it **does** do as well as what it doesn't. – Quentin Nov 27 '12 at 12:32
  • Also tell us what you are doing to cause the browser to make the request. – Quentin Nov 27 '12 at 12:33
  • To get this request i click on an ActionLink (modified my question for the source) I've tried return type ActionResult and File as returntype of my controller function. If i inspect the request made, with chrome, i see the controller answers with the correct json. But in browser nothing changes when not in specting it. Nothing is being written to the screen and no prompt for download.. – middelpat Nov 27 '12 at 12:38
  • @middelpat see my comment on my answer below... you need to use `@Html.ActionLink` instead of `@Ajax.ActionLink` – Johann Nov 27 '12 at 12:46

3 Answers3

1

Try something like this (set mime type to plain text) and a normal @Html.ActionLink:

public ActionResult GenerateOcJson()
{
var document = new { Data = jsonString, ContentType = "text/plain", FileName = String.Format("JSONResults_{0}.json", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")) };//... get from service layer
    var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = document.FileName,
        // Inline = false means always prompt the user for downloading.
        // Set it to true if you want the browser to try to show the file inline (fallback is download prompt)
        Inline = false,
    };
    Response.AppendHeader("Content-Disposition", cd.ToString());
    return File(document.Data, document.ContentType);
}
Tom Chantler
  • 14,117
  • 4
  • 46
  • 51
  • Tnx for your answer. I guess you mean ActionResult instead of ActionLink. But this doens't work for me. I don't have a file, so no path. What i want to download is just a string in memory. @johann's answer works for me. thnx for the effort! – middelpat Nov 27 '12 at 12:52
  • Yes I did mean ActionResult. And mine actually does work without a file. It uses the `jsonData` string and merely specifies a filename for the download. If you try it you should find that this is actually a correct answer! As is Johann's too, I believe :-) – Tom Chantler Nov 27 '12 at 13:23
  • I do believe with some code modifications on my side this answer will work just great, hence i upvoted this answer – middelpat Nov 27 '12 at 13:38
0

Just return a file result with the Application/Octet mediatype. No need to write the content-disposition header.

return File(Encoding.UTF8.GetBytes(jsonString),
            System.Net.Mime.MediaTypeNames.Application.Octet, 
            fileName);
Johann
  • 1,503
  • 10
  • 6
  • Tnx for the quick awnser. But unfortunately this doesn't work. I'm still stuck with the same behaviour – middelpat Nov 27 '12 at 12:34
  • Saw your edit where you mentioned it was an Ajax.ActionLink. Why ajax link? Just use a regular hyperlink that points to GenerateOcJson view. Since you want the user to download the json file, that is all you need. Ex: `@Html.ActionLink("Generate JSON", "GenerateOcJson")` – Johann Nov 27 '12 at 12:39
  • Based on everything you've told us, you don't need a post. – Johann Nov 27 '12 at 12:50
  • I don't, but after trying and trying you start trying such small things. But this actually works. Thnx! – middelpat Nov 27 '12 at 12:51
0

Use the generic mimetype of application/octet-stream. The browser will then treat the data as a file to download locally.

sargant
  • 346
  • 1
  • 16