【发布时间】:2017-10-03 08:58:23
【问题描述】:
对于最近的一个项目,我们正在尝试在 C# Umbraco 中创建一个可下载的文件,但我似乎无法让它工作。对于下载,我使用 System.Web.HttpContext.Current.Response。在 Umbraco CMS 中按下按钮后调用该函数。该函数被调用,但没有响应下载。该函数在继承自 System.Web.Http.ApiController 的类中实现。
我知道这个问题与其他一些问题相匹配,但我尝试了不同的解决方案,我觉得我忽略了一些东西。
源代码:
[Route("feedback")]
[HttpPost]
public void Add(FeedbackRequest feedback)
{
var Response = HttpContext.Current.Response;
string filePath = "C:\\testfile.txt";
var file = new FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
//Doesn't work with plain/text either
Response.ContentType = "application/force-download";
Response.Flush();
Response.TransmitFile(file.FullName);
try
{
Response.End();
}
catch (ThreadAbortException err)
{
}
catch (Exception err)
{
}
}
}
【问题讨论】:
-
您似乎在使用 ASP.NET MVC:从您的操作中返回
FilePathResult有什么问题? -
我没有使用 MvcController,所以我不能使用像 filepathresult 这样的方法。我的控制器是 ApiController。
标签: c# asp.net-web-api download httpcontext