views:

42

answers:

1

I thought MVC was supposed to make all this easier but I'm trying various ways and getting issues.

If I try the accepted answer for this question (changing the content type accordingly)... http://stackoverflow.com/questions/1375486/how-to-create-file-and-return-it-via-fileresult-in-asp-net-mvc

...I get into trouble because my encoding in the xml file is UTF-16.

The error I get is:

Switch from current encoding to specified encoding not supported.

This suggests that somewhere I need to tell MVC that I want UTF-16. Alternatively I want a different method that uses binary and not text.

A: 

This is what I've settled for:

public FileStreamResult DownloadXML()
{
    string name = "file.xml";
    XmlDocument doc = getMyXML();
    System.Text.Encoding enc = System.Text.Encoding.Unicode;
    MemoryStream str = new MemoryStream(enc.GetBytes(doc.OuterXml));

    return File(str, "text/xml", name);
}

I don't think this is perfect and I could probably just use a FileContentResult and not bother with the memory stream. Also, I don't think IE likes the unicode. It complains that "A name was started with an invalid character" despite the xml being fine and happily opening in firefox.

However it seems to do the job.

Chris Simpson