【发布时间】:2015-07-18 22:44:00
【问题描述】:
我正在使用下面的代码通过 ASP.NET 中的 Web API 进行下载。 当我尝试单击下载按钮时,它会调用 API。 执行“DownloadFile”-函数后,下载对话框没有出现。
[HttpGet]
public HttpResponseMessage DownloadFile(string DownloadFilePath)
{
HttpResponseMessage result = null;
var localFilePath = HttpContext.Current.Server.MapPath(DownloadFilePath);
// check if parameter is valid
if (String.IsNullOrEmpty(DownloadFilePath))
{
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
// check if file exists on the server
else if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{// serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = DownloadFilePath;
}
return result;
}
我没有从上面的代码中得到任何异常,但是下载文件的对话框没有出现。
【问题讨论】:
-
尝试在你的方法中加入try-catch,看看会不会有错误。
-
在 try catch 中没有得到任何异常
-
"rob waminal" 是否需要更改网页配置?
标签: javascript jquery asp.net asp.net-mvc asp.net-web-api