+ Reply to Thread
Results 1 to 2 of 2

Thread: file download from an external server?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    file download from an external server?

    I have an MVC controller method :
    Code:
    public ActionResult GetPdf(string filename)
            {
                var pdfDownload = File("~/store/Reports/report1.pdf", "application/pdf", Server.UrlEncode("report1.pdf"));
    
                return pdfDownload;
            }
    If i change the first parameter to the url of a server hosted on a separate cloud server then I get the error :

    Code:
    'MY FILE PATH' is not a valid virtual path.
    I just want my client to be able to download a file. This seems so much more complex than it need be.

  2. #2
    Actually, it's not that hard:
    Code:
    public ActionResult GetPdf(string filename)
    {
        using (var client = new WebClient())
        {
            var buffer = client.DownloadData("http://website.com/file.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.

    So let's solve those 2 serious problems by writing a custom action result code:
    Code:
    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://website.com/" + _filename))
            {
                stream.CopyTo(response.OutputStream);
            }
        }
    }
    That you will use like this:
    Code:
    public ActionResult GetPdf(string filename)
    {
        return new ReportResult(filename);
    }
    And in your view:
    Code:
    @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:
    Code:
    @Html.ActionLink("Download report", "GetPdf")
    You could have directly:
    Code:
    <a href="http://website.com/file.pdf">Download report</a>
    Assuming the client has access to this server of course.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts