C# 无法在客户端(MVC)下载文件
当我试图将excel文件从服务器下载到客户端时,没有收到任何响应。我有下面的代码来做同样的事情C# 无法在客户端(MVC)下载文件,c#,.net,file,model-view-controller,download,C#,.net,File,Model View Controller,Download,当我试图将excel文件从服务器下载到客户端时,没有收到任何响应。我有下面的代码来做同样的事情 public ActionResult Download() { string file = "testfile.xlsx"; string fullPath = Path.Combine(Server.MapPath("~/temp"), file); return File(fullPath, "application/vnd.openxmlformats-office
public ActionResult Download()
{
string file = "testfile.xlsx";
string fullPath = Path.Combine(Server.MapPath("~/temp"), file);
return File(fullPath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file);
}
我尝试将contenttype更改为“application/vnd.msexcel”,但仍然不起作用
代码执行此操作,但解析返回文件后不会发生任何事情。
我还将为此操作添加ajax调用
$('#Submit_Report').click(function (event) {
if (($('#txtReportMarket').val() == '') && ($('#txtReportPractice').val() == '')) {
alert("Please enter a value to Generate Report based on!");
event.preventDefault();
}
else {
$.ajax({
url: '/ShareTemplate/GenerateReport/',
data: "{ 'ReportPractice': '" + $('#txtReportPractice').val() + "','ReportMarket':'" + $('#txtReportMarket').val() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data["FileName"]);
$.ajax({
type: 'GET',
url: '/ShareTemplate/Download/',
success: function (data) {
alert("download Successful!");
@*window.open('@Url.Content("~/ShareTemplate/Download")')*@
},
error: function () {
DisplayError('Failed to load the data.');
}
});
}
});
}
});
第一个ajax调用工作正常,文件已经生成并存储。但是第二个ajax调用就是问题所在,有时我也会从第二个ajax调用中获得成功消息,但fle仍然没有下载。在第一个
ajax
调用中success
。放置下面的代码并删除剩余的代码
window.location = '@Url.Action("Download", "ShareTemplate")';