Solved

MVC 3.5 File Download

Posted on 2013-06-06
3
563 Views
Last Modified: 2013-06-15
Greetings.  Using MVC 3.5, I need to download a file, stored on the server, to the user's machine.  The destination path is unknown so a file dialog box should appear where the user can select the destination.  Here's the flow:

1.  User clicks a ActionLink which retrieves does some db calls to retrieve the source file path. - Done
2.  Still inside the ActionLink's method, a file dialog appears.  The user selects where the file will be saved on his local machine - Need Help Here
3.  Still inside the ActionLink's method, the site saves the file to the destination - Need Help Here.
4.  Action link method done.

Can you please provide working code that follows this flow?  Here's a public pdf that should be downloadable via the method  http://www.afplweb.com/images/stories/content_PDFs/Examples_of__Foundation_Investments.pdf or if you like to use your own that's fine.

Thank you much!
0
Comment
Question by:allenanderson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39225997
What have you read or tried? You do realize that we are here to assist, not write your code for you, yes?
0
 

Author Comment

by:allenanderson
ID: 39226859
Sure.  I've already made attempts using FilePathResult, FileStreamResult, and WebClient.  However, with all of these, even though some documentation and some examples suggest otherwise, I was never able to get the file dialog window to appear.  The destination needed to be already known.  What's required to cause the file dialog or the dialog where the user can open,save, or cancel the file?  Thanks again.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 39226964
You need to send back a Content-Disposition header with an attachement specifier. If you use the FileContentResult, then you can automagically add this header by setting the FileDownloadName property to the name that you want the user to see.

e.g.

using System.Web.Mvc;

namespace MvcApplication13.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/mypdftest.pdf")), "application/pdf");

            result.FileDownloadName = "test.pdf";

            return result;
        }
    }
}

Open in new window


Screenshot
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question