2

I have an XML file being generated filled with information from the Razor page, and I want to download this generated XML file on the click of a download button. I'm new to Razor pages, and returning the XML file as a FileResult isn't working for me. Guidance on what to write for my <a> and how to set up the C# annotations, etc would be very helpful.

My .cshtml code is:

<br/> 
@Html.ActionLink("Link name", "SaveFile", "EditLicense", 
        new { 
           LicenseFileJson = JsonConvert.SerializeObject(Model.License) 
        }) 
<br/> 

When this gets displayed on the page I get:

<br/> <a href="">Link name</a><br/> 

and clicking it does nothing.

My action code is:

public class EditLicenseController : Controller
{
    public FileResult SaveFile(string LicenseFileJson)
    {
        License License = (License)JsonConvert.DeserializeObject(LicenseFileJson);
        LicenseTool tool = new LicenseTool(License);
        string licenseFileString = tool.ToFileString();
        byte[] bytes = Encoding.ASCII.GetBytes(licenseFileString);
        var stream = new MemoryStream();
        var writer = new StreamWriter(stream);
        writer.Write(licenseFileString);
        writer.Flush();            
        Response.Headers.Add("Content-Disposition", "attachment;");
        return File(bytes, "text/xml", "testing123.xml");
    }
    ...

When I click the link I also don't see anything indicating this is working in the Network tab of chrome dev tools.

Alexander
  • 9,104
  • 1
  • 17
  • 41
creeeid
  • 23
  • 1
  • 9
  • 1
    Before you ask the question did you checked if there is a similar question with the answer? Did you write some code? There's a similar here, maybe it can help you: https://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult – Renan Mar 15 '19 at 19:03
  • My .cshtml code is: `@Html.ActionLink("Link name", "SaveFile", "EditLicenseController", new { LicenseFileJson = JsonConvert.SerializeObject(Model.License) })` When this gets displayed on the page I get: `Link name` and clicking it does nothing. – creeeid Mar 15 '19 at 19:15
  • Do you want download or "Save" the file. Could you add in the question the Action code from your Controller class? One tip: instead of add code in the comment, add it to your question :-) – Renan Mar 15 '19 at 19:23
  • thanks, new to stackoverflow. Added the code to my question :) – creeeid Mar 15 '19 at 19:32
  • also for clarification: I want to download the file – creeeid Mar 15 '19 at 20:01
  • Remove "Controller" from "EditLicenceController" in the action link: `@Html.ActionLink("Link name", "SaveFile", "EditLicense",...` – Mike Brind Mar 15 '19 at 20:12
  • @MikeBrind added more to the Action code for clarity – creeeid Mar 15 '19 at 20:24
  • The advice still stands – Mike Brind Mar 15 '19 at 20:25
  • I did it, still getting a blank href in the HTML it generates – creeeid Mar 15 '19 at 20:27

2 Answers2

1

Try to do like this:

Change the View:

@Html.ActionLink("Link name", "SaveFile", "EditLicense", new 
{ 
   LicenseFileJson = JsonConvert.SerializeObject(Model.License) 
});

Change the Action:

public FileResult SaveFile(string LicenseFileJson)
{
   License License = (License)JsonConvert.DeserializeObject(LicenseFileJson);
   LicenseTool tool = new LicenseTool(License);
   string licenseFileString = tool.ToFileString();
   //byte[] bytes = Encoding.ASCII.GetBytes(licenseFileString);
   //var stream = new MemoryStream();
   //var writer = new StreamWriter(stream);
   //writer.Write(licenseFileString);
   //writer.Flush();            
   //Response.Headers.Add("Content-Disposition", "attachment;");
   return File(System.Text.Encoding.UTF8.GetBytes(licenseFileString), "text/xml", "testing123.xml");
}

Edit:

Adding an example:

View:

@Html.ActionLink("link name", "SaveFile", "EditLicense", new
{
    LicenseFileJson = "SOME TEXT JUST TO TEST"
});

My link is rendered like: <a href="/EditLicense/SaveFile?LicenseFileJson=%22SOME%20TEXT%20JUST%20TO%20TEST%22">link name</a>

Action:

public FileResult SaveFile(string LicenseFileJson)
{            
   return File(System.Text.Encoding.UTF8.GetBytes(LicenseFileJson), "text/xml", "test");
}

Download successfully:

enter image description here

Renan
  • 204
  • 1
  • 8
  • Tried this and still nothing, is there some way I can modify a value or something inside the SaveFile function to tell if it's actually executing? – creeeid Mar 15 '19 at 21:13
  • I've just changed it to match your example and still nothing. It seems like my Razor page cannot see the method in the controller class, like I am missing a 'reference' or something. – creeeid Mar 15 '19 at 21:37
  • My link is rendering like this: `Link name` – creeeid Mar 15 '19 at 21:39
  • I've just edited the answer, adding a very simple sample. I'm not using JSON references, only passing a string for the action and returning a file to download. It works for me. If you use the same example must to work. – Renan Mar 15 '19 at 21:40
  • I just copy-pasted your example exactly, and I still don't get a proper href or any indication it's running. I'm still getting `link name` – creeeid Mar 15 '19 at 21:52
0

Empty href value means that it is something wrong with your routing configuration. Make sure that you have the following lines added in Startup.cs

app.UseMvcWithDefaultRoute();

or its equivalent

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});
Alexander
  • 9,104
  • 1
  • 17
  • 41
  • Made that change, still no results. Since it's not creating the href part of the anchor at all, would this mean it can't find the controller class to decide what link it needs to put there? Even when I try to manually navigate to site/EditLicense/SaveFile I still get nothing. – creeeid Mar 16 '19 at 00:51
  • I also made another test project and recreated this exact setup, and I'm still not getting any results. Could my installation be broken? – creeeid Mar 16 '19 at 00:51
  • @creeeid Push your project with the problem to github, I'll test it – Alexander Mar 16 '19 at 12:14
  • if you're still up for it @Alexander that would be great. [Github](http://github.com/carsonreid/DownloadTestApp) – creeeid Mar 18 '19 at 16:49
  • @creeeid in `Startup.cs` update line `app.UseMvc()` to `app.UseMvcWithDefaultRoute()` – Alexander Mar 18 '19 at 17:02
  • of course it was something that simple. Thanks a million :) feel free to add that as an answer and I'll mark it solved – creeeid Mar 18 '19 at 17:06