Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 MVC4允许用户下载文件(pdf)的最简单方式是什么_Asp.net Mvc 4_Pdf - Fatal编程技术网

Asp.net mvc 4 MVC4允许用户下载文件(pdf)的最简单方式是什么

Asp.net mvc 4 MVC4允许用户下载文件(pdf)的最简单方式是什么,asp.net-mvc-4,pdf,Asp.net Mvc 4,Pdf,我需要为我的用户提供一个文件,这个文件可以是pdf或xls文件 我似乎找不到一个简单的方法来做这件事 这就是我带来的: public FileResult DownloadPDF() { return view("/download/pdf1"); } 最快的方法是这样做: public ActionResult DownloadPdf() { return File("~/Download/pdf1.pdf", "applicatio

我需要为我的用户提供一个文件,这个文件可以是pdf或xls文件

我似乎找不到一个简单的方法来做这件事

这就是我带来的:

public FileResult DownloadPDF()
     {


         return view("/download/pdf1");
     }

最快的方法是这样做:

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

我找到的最好、最快的方法:

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        

        }

将文件路径传递到此方法,并根据文件类型更改内容类型。

如何将下载的文件命名为“TESTPDF1”?如下所示:return file(“~/Download/pdf1.pdf”,“application/pdf”,Server.UrlEncode(“TESTPDF1.pdf”);