Why is this so hard and how do i solve it ?
Actually, it's not that hard:
public ActionResult GetPdf(string filename)
{
using (var client = new WebClient())
{
var buffer = client.DownloadData("http://foo.com/bar.pdf");
return File(buffer, "application/pdf", "report1.pdf");
}
}
Now obviously there's a serious flaw with this method as it is buferring the file in memory. While this could work great for small reports, it could be problematic with large files and even more problematic if you have lots of users impatient to put their hands on this great report.
There's also another serious flaw with the first controller action. It mixes responsibilities. It contains infrastructure code and I challenge you to unit test it in isolation.
So let's solve those 2 serious problems by writing a custom action result:
public class ReportResult : ActionResult
{
private readonly string _filename;
public ReportResult(string filename)
{
_filename = filename;
}
public override void ExecuteResult(ControllerContext context)
{
var cd = new ContentDisposition
{
FileName = _filename,
Inline = false
};
var response = context.HttpContext.Response;
response.ContentType = "application/pdf";
response.Headers["Content-Disposition"] = cd.ToString();
using (var client = new WebClient())
using (var stream = client.OpenRead("http://foo.com/" + _filename))
{
// in .NET 4.0 implementation this will process in chunks
// of 4KB
stream.CopyTo(response.OutputStream);
}
}
}
that you will use like this:
public ActionResult GetPdf(string filename)
{
return new ReportResult(filename);
}
and in your view:
@Html.ActionLink("Download report", "GetPdf", new { filename = "report.pdf" })
Or you could totally question the usefulness of your controller action because in your view instead of:
@Html.ActionLink("Download report", "GetPdf")
you could have directly:
<a href="http://foo.com/bar.pdf">Download report</a>
assuming the client has access to this server of course.
Remark: be very careful with the filenames you are sending in the Content-Disposition
header. I see in your question that you used something like Server.UrlEncode("report1.pdf")
. Checkout the following question for the nightmare that this could turn into.