Solved

MVC 3.5 File Download

Posted on 2013-06-06
3
551 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but how can I download the file? 20 67
Name space syntax error 12 40
Message not shown 5 30
How to make a GridView cell hyperlinked using C# ? 3 14
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now