2013-12-10 57 views

回答

2

最快的方法是做這樣的事情:

public ActionResult DownloadPdf() 
    { 
    return File("~/Download/pdf1.pdf", "application/pdf", Server.UrlEncode("NameOfFile.pdf")); 
    } 
+1

我怎樣才能讓下載文件的名稱爲「TESTPDF1」? – user3086989

+0

像這樣:return File(「〜/ Download/pdf1.pdf」,「application/pdf」,Server.UrlEncode(「TESTPDF1.pdf」)); – mmilan

0

最佳和最快的方式,我發現:

public void DownloadReport(string path) 
     { 
      // Clear the content of the response 
      Response.ClearContent(); 
      FileInfo newFile = new FileInfo(path); 

      string fileName = Path.GetFileNameWithoutExtension(newFile.Name) + DateTime.Now + newFile.Extension; 

      // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header 
      Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 

      // Add the file size into the response header 
      Response.AddHeader("Content-Length", newFile.Length.ToString()); 

      // Set the ContentType 
      Response.ContentType = "application/vnd.ms-excel"; 

      // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) 
      Response.TransmitFile(newFile.FullName); 

      // End the response 
      System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); 

      //send statistics to the class   

     } 

將文件的路徑傳遞給此方法&根據您的文件更改內容類型e類型。

相關問題