Skip to content Skip to sidebar Skip to footer

How To Initialize File Download From An External Server?

I have an MVC controller method defined like this: public ActionResult GetPdf(string filename) { var pdfDownload = File('~/Content/GeneratedReports/report1.pdf

Solution 1:

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:

publicclassReportResult : ActionResult
{
    privatereadonlystring _filename;
    publicReportResult(string filename)
    {
        _filename = filename;
    }

    publicoverridevoidExecuteResult(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)
{
    returnnew 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:

<ahref="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.

Solution 2:

You could just redirect the user at the remote report; if that isn't an option, you will need to proxy it:

byte[] blob;
using(var client = new WebClient()) {
    blob = client.DownloadData(remoteUrl);
}
returnFile(blob, "application/pdf", "report1.pdf");

the above assumed the file isn't very big; a more robust implementation would fetch and send in chunks.

Post a Comment for "How To Initialize File Download From An External Server?"