我正在尝试从HttpResponseMessage类型的WEB API Get方法下载一个excel文件。 我可以通过调用AJAX来实现该方法,该方法还返回resultcontent,但它不会在浏览器中下载文件。我尝试了window.location,它重定向到一个新页面,提示-“该网站无法显示该页面” 。我尝试通过警告成功与错误来进行调试,它以[Object object]的形式警告错误。下面是我的代码,请更正我要去的地方。谢谢。
JavaScript
$(document).ready(function () {
$("#btnDownload").click(function () {
var apiUrl = "../api/DownloadExcel/ExportExcelFile?OriginalRequestNumber=";
var originalReqIdentifier = $('#OriginalRequestNumber').val();
$.ajax({
url: apiUrl + originalReqIdentifier,
type: 'GET',
dataType: 'json',
success: function (data) {
alert(data);
},
error: function (data) {
alert('hi');
}
});
});
});
HTML
<input href="#" class="btn" type="Submit" id="btnDownload" name="btnDownload" value="Download" />
c#
public class DownloadExcelController : ApiController
{
private IExcelExport _excelExport { get; set; }
public DownloadExcelController()
{
_excelExport = new GenerateExcel();
}
// GET api/DownloadExcel/ExportExcelFile
[HttpGet]
public HttpResponseMessage ExportExcelFile(string OriginalRequestNumber)
{
var ObjectToExcel = new List<DummyExternalLoginViewModel>
{
new DummyExternalLoginViewModel { Name = "Mohammed", FamilyName= "Ansari", State = "CA"},
new DummyExternalLoginViewModel { Name = "Harvey", FamilyName= "Spectre", State = "NY"},
new DummyExternalLoginViewModel { Name = "Mike", FamilyName= "Ross", State = "NY"},
new DummyExternalLoginViewModel { Name = "Donald", FamilyName= "Trump", State = "AL"},
new DummyExternalLoginViewModel { Name = "Spencer", FamilyName= "Mike", State = "AK"},
new DummyExternalLoginViewModel { Name = "Trump", FamilyName= "Donald", State = "AZ"},
new DummyExternalLoginViewModel { Name = "Bill", FamilyName= "Gates", State = "AR"}
};
var resultContent = _excelExport.Export(ObjectToExcel, "ExcelExport", true);
return resultContent;
}
}
答案 0 :(得分:1)
您双方都有问题-客户端和服务器端:
使用同步文件下载(或按照Download a file by jQuery.Ajax进行异步):
$(document).ready(function () {
$("#btnDownload").click(function () {
var apiUrl = "../api/DownloadExcel/ExportExcelFile?OriginalRequestNumber=";
var originalReqIdentifier = $('#OriginalRequestNumber').val();
window.location = apiUrl + originalReqIdentifier;
});
});
定义HttpResponseMessage的必需属性:
[HttpGet]
public HttpResponseMessage ExportExcelFile(string OriginalRequestNumber)
{
// ..
var resultContent = _excelExport.Export(ObjectToExcel, "ExcelExport", true);
var stream = new MemoryStream(resultContent);
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) };
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "file.xlsx" };
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
return response;
}
答案 1 :(得分:0)
<input type="radio" id = "choice1" name="snooze" value="samplechoice0" onClick='valuechanged();'/> samplechoice0<br>
<input type="radio" id = "choice2" name="snooze" value="samplechoice1" onClick='valuechanged();'/> samplechoice1<br>
<input type="radio" id = "choice3" name="snooze" value="samplechoice2" onClick='valuechanged();'/> samplechoice2 <br>
<input type="radio" id = "choice10" name="snooze" value="Normal Radio" onClick='valuechanged();'/>
<input type="text" id = "choice11" class="tb" name="tb1" placeholder="Enter Other Reason Here" onkeypress='valuechanged();' > <br>
function Submit() {
var items=document.getElementsByClassName('radio');
var selectedItems=" ";
for(var i=0; i<items.length; i++) {
if(items[i].type=='radio' && items[i].checked==true && document.getElementById('choice10').checked==false) {
selectedItems+=items[i].value+"; ";
}
}
if(document.getElementById("choice10").checked == true) {
selectedItems = document.getElementById('choice11').value;
}
alert(selectedItems);
}
放置 window.location = apiUrl + originalReqIdentifier;在Ajax错误中,为我工作。