2016-11-18 54 views
1

我在服務器上有一個PDF文件,我想將其作爲下載發送回瀏覽器,但是它只是加載到屏幕上。這是我在做什麼:下載PDF文件,不要在屏幕上顯示

public ActionResult BillOfMaterials(Model model) 
{ 
    .... 
    return File(@"C:\...(file path)...\result.pdf", "application /pdf"); 
} 

我需要做什麼,以便它實際上只是下載,並不會在瀏覽器中打開?

謝謝!

+3

這是你在找什麼? https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc?rq=1 – ElectricRouge

+1

也許這可能會幫助你:http://stackoverflow.com /問題/ 8590543 /力瀏覽器對下載的PDF文檔,而不是-的開口,它 –

回答

0

這樣做了。

  string filepath = @"C:\docs\result.pdf"; 
      byte[] filedata = System.IO.File.ReadAllBytes(filepath); 
      string contentType = System.Web.MimeMapping.GetMimeMapping(filepath); 

      var cd = new System.Net.Mime.ContentDisposition 
      { 
       FileName = "result.pdf", 
       Inline = false, 
      }; 

      Response.AppendHeader("Content-Disposition", cd.ToString()); 

      return File(filedata, contentType);