0

In my application I want to give user the option to download a PDF file. In my code, the file gets opened by browser; however, I want the file to be downloaded. Here's my code:

Controller

string name = id; //id is the name of the file
string contentType = "application/pdf";

var files = objData.GetFiles(); //list of files


string filename = (from f in files
                   orderby f.DateEncrypted descending
                   where f.FileName == name
                   select f.FilePath).First(); //gets the location of the file

string FullName = (from f in files
                   where f.FileName == name
                   select f.FileName).First(); //gets the new id in new location to save the file with that name



//Parameters to File are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(filename, contentType, FullName);

Here's how I'm using it in dropdown menu.

View:

<li><a id="copyURL" href="@Url.Action("Download", "Home", new { id = item.FileName})">Download</a></li>

By clicking on "Download", the file gets opened browser.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3853986
  • 15
  • 1
  • 1
  • 8

3 Answers3

0

Set your content type to "application/octet-stream" so the PDF plugin won't try to pick it up and display it. Then the browser will handle it as a file download.

  • The browser still tries to open it. I've tried it with Chrome and IE. – user3853986 Sep 30 '14 at 22:24
  • Can you try adding this line before your return statement? `Response.AddHeader("content-disposition", "attachment; filename="+filename);` – Eric Mattison Sep 30 '14 at 22:45
  • Still the same. I've tried Response.AddHeader("content-disposition", "attachment; filename="+filename); and Response.AddHeader("content-disposition", "attachment; filename="+FullName); – user3853986 Sep 30 '14 at 23:01
  • Try referencing this [question](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc), it is a bit old but should still be relevant. – Eric Mattison Sep 30 '14 at 23:07
  • @user3853986 the System.Net.Mime.ContentDisposition class has Inline property try to set it to true – Marian Ban Oct 01 '14 at 06:52
0

Download Files from Web:

This example shows how to download files from any website to local disk. The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file. Download File Synchronously

The following code shows how to download file synchronously. This method blocks the main thread until the file is downloaded or an error occur (in this case the WebException is thrown). [C#]:

using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("pdf file address", @"c:\myfile.pdf");

Download File Asynchronously: To download file without blocking the main thread use asynchronous method DownloadFileA­sync. You can also set event handlers to show progress and to detect that the file is downloaded. [C#]:

private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  webClient.DownloadFileAsync(new Uri("pdf file address"), @"c:\myfile.pdf");
}

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
  progressBar.Value = e.ProgressPercentage;
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}

ref: http://www.csharp-examples.net/download-files/

Power Man
  • 166
  • 1
  • 10
0

Browser will try to show the file unless you specify not to.

Try Adding ContentDisposition before returning File.

var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = filename, 
        Inline = false, 
    };
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filename, contentType, FullName);
Yusuf Demirag
  • 773
  • 7
  • 10