Solved

MVC 3.5 File Download

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net web application 3 58
Why use this lambda? 12 96
make control visible based off other control value 7 45
How do I "share" on social sites? 2 39
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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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