如何在asp.net MVC中生成链接以下载文件?

时间:2022-11-23 09:59:50

I am testing Doddle Report to generate a few report form a IEnumerable object. I need to generate a Link like this

我正在测试Doddle Report以生成一个IEnumerable对象的报告。我需要像这样生成一个链接

PDF - http://myserver.com/reports/ProductsReport.pdf

The Question is how do I do this?

问题是我该怎么做?

En Stop Using Doddle Report, and generate del Excel in XML format.

En停止使用Doddle报告,并以XML格式生成del Excel。

4 个解决方案

#1


Check out this tutorial to return different action result.

查看本教程以返回不同的操作结果。

The ASP.NET MVC framework supports several types of action results including:

ASP.NET MVC框架支持几种类型的操作结果,包括:

  • ViewResult – Represents HTML and markup.
  • ViewResult - 表示HTML和标记。

  • EmptyResult – Represents no result.
  • EmptyResult - 表示无结果。

  • RedirectResult – Represents a redirection to a new URL.
  • RedirectResult - 表示重定向到新URL。

  • JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • JsonResult - 表示可以在AJAX应用程序中使用的JavaScript Object Notation结果。

  • JavaScriptResult – Represents a JavaScript script.
  • JavaScriptResult - 表示JavaScript脚本。

  • ContentResult – Represents a text result.
  • ContentResult - 表示文本结果。

  • FileContentResult – Represents a downloadable file (with the binary content).
  • FileContentResult - 表示可下载的文件(包含二进制内容)。

  • FilePathResult – Represents a downloadable file (with a path).
  • FilePathResult - 表示可下载文件(带路径)。

  • FileStreamResult – Represents a downloadable file (with a file stream).
  • FileStreamResult - 表示可下载文件(带有文件流)。

#2


I'd done something similar what you want on MVC 5 and I used FilePathResult as J.W said.

我在MVC 5上做了类似你想要的东西,我使用了FilePathResult,就像J.W所说的那样。

cshtml:

@Html.ActionLink("Download Here", "DownloadExampleFiles", "Product", new { fileName = "variation-example" }, new { @target = "_blank" })

ProductController:

public FilePathResult DownloadExampleFiles(string fileName)
{
    return new FilePathResult(string.Format(@"~\Files\{0}", fileName + ".txt"), "text/plain");
}

Download a file seems to be very silly/easy, however, for those who are new on MVC, it's not. This approach sounds to be the best while using MVC.

下载一个文件似乎非常愚蠢/容易,然而,对于那些刚接触MVC的人来说,事实并非如此。使用MVC时,这种方法听起来是最好的。

PS: I've forced a .txt just as an example. You should do what is intersting for you to get the specific file.

PS:我强迫.txt就是一个例子。您应该为您获取特定文件的内容。

#3


AFAIK, there is nothing special about doing this in MVC. Create a LinkButton on your form with a handler that generates the PDF file, then redirect to the created file.

AFAIK,在MVC中这样做没有什么特别之处。使用生成PDF文件的处理程序在表单上创建LinkBut​​ton,然后重定向到创建的文件。

#4


Something like this maybe:

这样的事情可能是:

<h2>List Of Reports</h2>
<ul>
    <% foreach (var report in Model){ %>
        <li><a href="<%= Html.BuildUrlFromExpression<ReportController>(r => r.Reports(report.Filename)) %>"><%=report.Filename %></a></li>
    <% } %>
</ul>

There maybe a shorter way but this works.

可能有一个较短的方法,但这是有效的。

#1


Check out this tutorial to return different action result.

查看本教程以返回不同的操作结果。

The ASP.NET MVC framework supports several types of action results including:

ASP.NET MVC框架支持几种类型的操作结果,包括:

  • ViewResult – Represents HTML and markup.
  • ViewResult - 表示HTML和标记。

  • EmptyResult – Represents no result.
  • EmptyResult - 表示无结果。

  • RedirectResult – Represents a redirection to a new URL.
  • RedirectResult - 表示重定向到新URL。

  • JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • JsonResult - 表示可以在AJAX应用程序中使用的JavaScript Object Notation结果。

  • JavaScriptResult – Represents a JavaScript script.
  • JavaScriptResult - 表示JavaScript脚本。

  • ContentResult – Represents a text result.
  • ContentResult - 表示文本结果。

  • FileContentResult – Represents a downloadable file (with the binary content).
  • FileContentResult - 表示可下载的文件(包含二进制内容)。

  • FilePathResult – Represents a downloadable file (with a path).
  • FilePathResult - 表示可下载文件(带路径)。

  • FileStreamResult – Represents a downloadable file (with a file stream).
  • FileStreamResult - 表示可下载文件(带有文件流)。

#2


I'd done something similar what you want on MVC 5 and I used FilePathResult as J.W said.

我在MVC 5上做了类似你想要的东西,我使用了FilePathResult,就像J.W所说的那样。

cshtml:

@Html.ActionLink("Download Here", "DownloadExampleFiles", "Product", new { fileName = "variation-example" }, new { @target = "_blank" })

ProductController:

public FilePathResult DownloadExampleFiles(string fileName)
{
    return new FilePathResult(string.Format(@"~\Files\{0}", fileName + ".txt"), "text/plain");
}

Download a file seems to be very silly/easy, however, for those who are new on MVC, it's not. This approach sounds to be the best while using MVC.

下载一个文件似乎非常愚蠢/容易,然而,对于那些刚接触MVC的人来说,事实并非如此。使用MVC时,这种方法听起来是最好的。

PS: I've forced a .txt just as an example. You should do what is intersting for you to get the specific file.

PS:我强迫.txt就是一个例子。您应该为您获取特定文件的内容。

#3


AFAIK, there is nothing special about doing this in MVC. Create a LinkButton on your form with a handler that generates the PDF file, then redirect to the created file.

AFAIK,在MVC中这样做没有什么特别之处。使用生成PDF文件的处理程序在表单上创建LinkBut​​ton,然后重定向到创建的文件。

#4


Something like this maybe:

这样的事情可能是:

<h2>List Of Reports</h2>
<ul>
    <% foreach (var report in Model){ %>
        <li><a href="<%= Html.BuildUrlFromExpression<ReportController>(r => r.Reports(report.Filename)) %>"><%=report.Filename %></a></li>
    <% } %>
</ul>

There maybe a shorter way but this works.

可能有一个较短的方法,但这是有效的。