Asp.net mvc 在下拉列表中填写下载更改Jquery
我正在下载一个关于DropDownChange的excel文件。 下面是我的下拉列表Asp.net mvc 在下拉列表中填写下载更改Jquery,asp.net-mvc,.net-core,Asp.net Mvc,.net Core,我正在下载一个关于DropDownChange的excel文件。 下面是我的下拉列表 <select style="float: right; width: 15%;" id="templateDropDown" class="form-control"> <option value="select">Select</option> <op
<select style="float: right; width: 15%;" id="templateDropDown" class="form-control">
<option value="select">Select</option>
<option value="Movies">Movies</option>
<option value="TV_SHOWS">TV SHOWS</option>
</select>
<label style="float: right;padding: 0.3%">Download Template</label>
我返回的文件以字节为单位,但它没有下载文件。我也没有收到任何运行时错误。有人能告诉我我丢失了什么吗
提前感谢如果您通过AJAX检索文件,最简单的答案不是返回文件,而是返回文件URL并更改浏览器位置以开始下载。这是一项棘手的工作 (作为旁注,您不需要
$('document')。ready
包装器-$('#templateDropDown')
已经是这方面的缩写。)
$('#templateDropDown')。更改(函数(){
showSpinner();
var templateType=$('option:selected').val();
$.ajax({
url:'@url.Action(“下载模板”,“下载”),
数据:{templateType:templateType},
键入:“POST”,
成功:功能(结果){
hideSpinner();
window.location.href=结果;
},
错误:函数(){
}
});
});
控制器:
公共字符串下载模板(字符串模板类型)
{
if(templateType==电影)
{
var filePath=Path.Combine(_hostingEnvironment.WebRootPath,“Templates\\Movies.xlsx”);
}
其他的
{
var filePath=Path.Combine(_hostingEnvironment.WebRootPath,“Templates\\TVSHOWS.xlsx”);
}
返回文件路径;
}
<script type="text/javascript">
$("document").ready(function() {
$('#templateDropDown').change(function () {
showSpinner();
var templateType = $('option:selected').val();
$.ajax({
url: '@Url.Action("DownloadTemplate","Download")',
data: { templateType: templateType },
type: 'POST',
success: function(result) {
hideSpinner();
},
error: function() {
}
});
});
});
</script>
public FileContentResult DownloadTemplate(string templateType)
{
if(templateType == Movies)
{
var fileAllBytes = System.IO.File.ReadAllBytes(Path.Combine(_hostingEnvironment.WebRootPath, "Templates\\Movies.xlsx"));
var filePath = Path.Combine(_hostingEnvironment.WebRootPath, "Templates\\Movies.xlsx");
}else{
var fileAllBytes = System.IO.File.ReadAllBytes(Path.Combine(_hostingEnvironment.WebRootPath, "Templates\\TVSHOWS.xlsx"));
var filePath = Path.Combine(_hostingEnvironment.WebRootPath, "Templates\\TVSHOWS.xlsx");
}
HelperExtensions.GetFileContentTypeProvider(filePath, out var contentType);
return File(fileAllBytes, contentType, "Movies.xlsx");
}