Reputation: 7254
Short version: I have a third-party MVC Extension that returns the FileResult. I would like to be able to process the file before returning it to the client. How can I do that?
If you're interested in boring details, read below:
I'm using Devexpress XtraReports for reporting purposes. The problem is that it's PDF Export is terrible, but RTF one works great. I would like to create action&result filter that would trick DevExpress into generating rtf instead of pdf (done) and convert the rtf to pdf using some other third-party library. The only problem is that I need to obtain rtf file from FileResult and return my own FileResult with the converted content.
//EDIT: The code right now looks as follows:
public virtual ActionResult ReportExport(TParameters parameters)
{
return DevExpress.Web.Mvc.ReportViewerExtension.ExportTo(this.GetReport(parameters));
}
Upvotes: 2
Views: 2843
Reputation: 32758
You said you are using a third-party MVC extension that returns a FileResult
and you want to access the file details that are wrapped inside the result.
If you see the FileResult
, it is an abstract class and I just know there are three built-in implementations are available in MVC (I don't aware about others): FileContentResult
, FileStreamResult
and FilePathResult
.
All these classes provide public properties to access the details of the file. For ex. the FileContentResult
contains a public property called FileContents
that returns the content of a file as a byte array.
The third-party extension can return any FileResult
implementation of the built-in types or badly(if you can't access those type) their own types as well. You can check the returned FileResult
instance's type and cast it accordingly to access the file details.
var fileResult = ... returned by the MVC extension
if(fileResult is FileContentResult)
{
var fileContentResult = fileResult as FileContentResult;
var fileContent = fileContentResult.FileContents;
// process the file
}
else if(fileResult is FileStreamResult)
{
var fileStreamResult = fileResult as FileStreamResult;
var fileStream = fileStreamResult .Stream;
//...
}
else if(fileResult is FilePathResult) // this result only contains the path
// of the file name
{
//...
}
If you want make the things reusable you can implement a custom file result that takes the FileResult
returned by the extension in the constructor and override the WriteFile
method.
Upvotes: 4
Reputation: 32715
The FileResult probably writes the file contents to the response stream. You can use a standard ASP.NET response stream using a filter. See this ancient article for details:
https://web.archive.org/web/20211029043851/https://www.4guysfromrolla.com/articles/120308-1.aspx
Alternatively you could create your own ActionResult that invokes the DevExpress FileResult using a custom HttpContext with a fake response stream, which is where you would do your conversion.
Upvotes: 3