This project has moved and is read-only. For the latest updates, please go here.

Override the output file name using MVC?

Dec 6, 2011 at 2:30 AM

I'm using the MVC ReportResult.  Is there a way to override the file name?  It seems like the code assumes the action name should be the file name, but that's not ideal in my case.

Dec 6, 2011 at 7:37 PM

I am going to add this feature right now, but I'm curious on your usage. Are you still using the file extension feature to determine which type of report to render, or do you want to specific the Writer AND filename together?

Dec 6, 2011 at 8:19 PM

Wow, thanks!

 

Right now, I'm using the web.config with 

defaultWriter="ExcelOpenXml"

so I can have an extensionless route.  I'm currently not specifying the IReportWriter in ReportResult.

 

Also, I had to change the contentType in the web.config.  I downloaded from NuGet with VS2010:

<add format="ExcelOpenXml" type="DoddleReport.OpenXml.ExcelReportWriter, DoddleReport.OpenXml" contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" offerDownload="true" fileExtension=".xlsx" />

Dec 6, 2011 at 8:20 PM

Wow that's a monster contentType, haha, is that what it should be? Do you have a URL that describes that the openXML content types are for each type of file? If that is correct, I will fix that bug in the package as well

Dec 6, 2011 at 8:24 PM

Here is the API I am proposing, please confirm if it will solve your scenario

There is a new FileName property on ReportResult which has 3 usages

        /// <summary>
        /// This property is optional. 
        /// If you don't specify a FileName then the name of the ActionResult being executed will be used. 
        /// If you do specify a FileName, you may omit the file extension. If the file extension is omitted then DoddleReport will attempt to get the extension from the URL being requested
        /// </summary>
        public string FileName { get; set; }

 

public class TestController : Controller
{
    public ReportResult WithExtension()
    {
        var query = DoddleProductRepository.GetAll();
        var report = new Report(query.ToReportSource());
        return new ReportResult(report) { FileName = "CustomName.html"};
    }

    public ReportResult WithExtensionAndWriter()
    {
        var query = DoddleProductRepository.GetAll();
        var report = new Report(query.ToReportSource());
        return new ReportResult(report, new DelimitedTextReportWriter()) { FileName = "CustomName.txt" };
    }

    public ReportResult WithoutExtension()
    {
        var query = DoddleProductRepository.GetAll();
        var report = new Report(query.ToReportSource());
        return new ReportResult(report) { FileName = "CustomName" };
    }
}
Dec 6, 2011 at 8:35 PM

Yes, I think so.  Just having it exposed as an optional property sounds good.  It doesn't need to be another constructor overload.

I'm out of my depth on the contentType.  I just had it not work, Googled, changed it, and got it working.  Here's a link with that mime type http://en.wikipedia.org/wiki/Internet_media_type for Excel.  Can this perhaps be verified with Louis-Philippe Perras before putting this change in NuGet?

Dec 6, 2011 at 8:44 PM

Looks like you're correct. I've updated the source and will fix this in the NuGet package

http://blogs.msdn.com/b/dmahugh/archive/2006/08/08/692600.aspx