Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC 3.5 File Download

Posted on 2013-06-06
3
Medium Priority
?
573 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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

876 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