Solved

MVC 3.5 File Download

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

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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Javascript - getdate 5 51
Required permissions cannot be acquired 6 38
DLL in ASP.NET 20 44
Asp.net mvc 5 5 19
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

21 Experts available now in Live!

Get 1:1 Help Now