Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:33+00:00 2026-05-28T14:07:33+00:00

I have an MVC controller method defined like this: public ActionResult GetPdf(string filename) {

  • 0

I have an MVC controller method defined like this:

public ActionResult GetPdf(string filename)
        {
            var pdfDownload = File("~/Content/GeneratedReports/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 :

‘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.

I have a URL that points to a PDF. I want my client to download that pdf without clicking anything. (The download will initiate upon successful service response)

Why is this so hard and how do i solve it ?

I don’t care if the solution is in JS or MVC….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:07:34+00:00Added an answer on May 28, 2026 at 2:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
If I have a controller like this: [HttpPost] public JsonResult FindStuff(string query) { var
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
I have a controller method with a custom FilterAttribute on it... [ActivityHistory] public ActionResult
Hi I've got problem with foreach statement. I have my controller method: public ActionResult
i have a quetion about MVC routing. I have a controller method that is
I have a controller in MVC serving up images from a database. EDIT: This
I have a function defined in a module called DataAccess like this module DataAccess
I currently have a Spring MVC controller that takes a MultipartFile @RequestMapping(method = RequestMethod.POST)
I have the following where I'm trying to send list/array to MVC controller method:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.