【发布时间】:2019-01-08 17:01:58
【问题描述】:
FilePathResult 没有下载文件
我尝试返回 File(...),尝试将文件更改为空白工作簿,更改了文件的位置
public ActionResult GenerateSupplyInformation(List<string> serialNumbers)
{
if (serialNumbers != null)
{
ViewBag.strmsg = "";
foreach (var serialNumber in serialNumbers)
{
if (!string.IsNullOrWhiteSpace(serialNumber))
{
var results = _ftRepository.GetSupplyInformation(serialNumber);
var filePath = _documentGenerator.GenerateSupplyInformation(serialNumber, results);
return new FilePathResult(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
}
}
return null;
}
我希望当这个方法被点击时文件会下载,因为没有抛出异常
这是问题的更新版本
这里是去掉了下载文件调用的 ajax 调用
$.ajax(
{
type: "POST",
//url: "/JQueryAjaxCall/AjaxPostCall",
url: "/RWM/GenerateSupplyInformation",
data: JSON.stringify(models),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
//alert("response");
}
}
);
这里是 new(er) 可能略有不同,虽然它的行为相同,控制器方法
public ActionResult GenerateSupplyInformation(List<LxStockDataModel> records)
{
try
{
if (records != null)
{
ViewBag.strmsg = "";
foreach (var serialNumber in records)
{
if (!string.IsNullOrWhiteSpace(serialNumber.Serial_No))
{
var results = _ftRepository.GetSupplyInformation(serialNumber.Serial_No);
var filePath = _documentGenerator.GenerateSupplyInformation(serialNumber.Serial_No, results);
var file = new FilePathResult(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//return file;
return File(filePath, System.Net.Mime.MediaTypeNames.Application.Octet);
}
}
return View("RWMSummary", records);
}
}
catch (Exception ex)
{
_log.Exception(ex);
}
return null;
}
无论是方法中的返回,File 还是 FilePathResult,我都会得到文件不会下载的相同行为。我一定是做错了什么??
【问题讨论】:
标签: c# asp.net-mvc download