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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/745042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:29:14  来源:igfitidea点击:

How to Generate a Link to download a File in asp.net MVC?

asp.net-mvcdownload

提问by Jedi Master Spooky

I am testing Doddle Reportto 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 Report,生成 XML 格式的 del Excel。

回答by J.W.

Check out this tutorialto return different action result.

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

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

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

  • ViewResult – Represents HTML and markup.
  • EmptyResult – Represents no result.
  • RedirectResult – Represents a redirection to a new URL.
  • JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • JavaScriptResult – Represents a JavaScript script.
  • ContentResult – Represents a text result.
  • FileContentResult – Represents a downloadable file (with the binary content).
  • FilePathResult – Represents a downloadable file(with a path).
  • FileStreamResult – Represents a downloadable file (with a file stream).
  • ViewResult – 代表 HTML 和标记。
  • EmptyResult – 表示没有结果。
  • RedirectResult – 表示重定向到新 URL。
  • JsonResult – 表示可在 AJAX 应用程序中使用的 JavaScript 对象表示法结果。
  • JavaScriptResult – 代表一个 JavaScript 脚本。
  • ContentResult – 表示文本结果。
  • FileContentResult – 表示可下载的文件(带有二进制内容)。
  • FilePathResult –表示可下载的文件(带有路径)。
  • FileStreamResult – 表示可下载的文件(带有文件流)。

回答by Adriano Galesso Alves

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

我在 MVC 5 上做了类似的事情,我使用了 JW 所说的 FilePathResult 。

cshtml:

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 作为示例。你应该做一些让你感兴趣的事情来获取特定的文件。

回答by Dave Swersky

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,然后重定向到创建的文件。

回答by darren

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.

也许有更短的方法,但这是有效的。