在我的ASP.NET MVC项目中,我使用ClosedXML生成了一个excel文件.
它在非ajax调用中运行良好.这是我的控制器动作方法
// Prepare the response
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=\"" + reportHeader + ".xlsx\"");
// Flush the workbook to the Response.OutputStream
using (MemoryStream memoryStream = new MemoryStream())
{
MyWorkBook.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
memoryStream.Close();
}
Response.End();
Run Code Online (Sandbox Code Playgroud)
现在我试图通过ajax请求来做到这一点.但该文件不是从mvc控制器发送的.
$.ajax({
url: url,
type: "POST",
data: fd,
processData: false,
contentType: false,
beforeSend: function () {
},
success: function (response) {
},
error: function (request, status, error) {
},
complete: function () {
}
});
Run Code Online (Sandbox Code Playgroud)
我怎么能完成它?先感谢您.
在我的MVC项目中,我有一个与Knockout绑定的HTML表.
我正在尝试将表导出到Excel.
我尝试在客户端使用JavaScript:
self.exportToExcel = function () {
javascript: window.open('data:application/vnd.ms-excel,' + $("#tableToprint").innerHTML());
}
Run Code Online (Sandbox Code Playgroud)
要么:
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table cellspacing="0" rules="rows" border="1" style="color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:9pt;text-align:center;">{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name …Run Code Online (Sandbox Code Playgroud) 我目前有以下工作代码(angular但适用于任何JS框架):
var url = '/endpoint/to/my/file';
$http({
method: 'GET',
url: url
})
.success(function(jdata) {
window.location = url;
})
.error(function(je){
// display errors on page
});
Run Code Online (Sandbox Code Playgroud)
在表单完成并且用户点击"提交"之后调用上面的内容(实际情况比这更复杂,但它是相同的想法).我异步进行表单检查,因此没有页面重新加载.
如果请求成功,则返回二进制文件(pdf文件),如果不成功,请求将返回400 BadRequest,并在JS中格式化错误.所以我做的是,如果成功,我重定向到相同的URL以获得PDF,否则我得到JSON错误对象并用它做一些事情.
如果请求成功,我怎么能避免提出两个请求?
"CONTENT-DISPOSITION" -> "attachment"在成功响应的HTTP头中更新:有关Emile请求的体系结构的其他信息:在我的用例中,我有一个端点检查输入(和其他外部要求).出于安全原因,如果不满足所有要求,我将无法输出PDF,因此无论如何我必须在交付文件(文件自动生成)之前进行检查.因此,拥有两个端点只会是多余的,并增加一些不必要的复杂性.
在编写时我认为另一种解决方案可能是在执行检查时在端点上传递参数,这样如果成功,它会停止并且不会生成PDF,然后重定向到相同的端点,而不会输出将输出PDF的标志.所以我做了两次检查,但只加载(并生成 - 这是资源密集型)文件只有一次,我只有一个端点......
这是改编的代码:
var url = '/endpoint/to/my/file';
$http({
method: 'GET',
url: url+'?check'
})
.success(function(jdata) {
window.location = url;
})
.error(function(je){
// display errors on page
});
Run Code Online (Sandbox Code Playgroud)
在后端(我使用Play框架/ Scala)
def myendpoint(onlyDoCheck: Boolean = false) = Action{implicit request =>
myForm.bindFromRequest.fold(
e => BadRequest(myErrors),
v …Run Code Online (Sandbox Code Playgroud) 我有file.zipDB喜欢的BLOB.我想在Spring控制器中创建方法,以便在客户端下载此文件.
@RequestMapping(value = "/downloadResolution/{resolutionId}", method = RequestMethod.GET)
public void downloadResolution(@PathVariable("resolutionId") Long resolutionId, HttpServletResponse response) {
Resolution resolution = resolutionService.findOne(resolutionId);
ResolutionArchive resolutionArchive = resolution.getResolutionArchive();
if (resolutionArchive == null) return;
byte[] archive = resolutionArchive.getArchive();
//this byte[] archive - my zip file from db
}
Run Code Online (Sandbox Code Playgroud)
如何更改此methot以便在客户端下载?
用户按下载按钮.Methos从byte []中获取数据,用户可以下载它.
编辑
我尝试了@pleft的解决方案,它的工作原理.我知道 - 我使用ajax进行呼叫方法
function downloadResolution(resulutionId) {
$.ajax({
type: 'GET',
dataType: "json",
url: '/downloadResolution/' + resulutionId,
success: function (data) {
},
error: function (xhr, str) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果我使用ajax,怎么意识到这一点?
asp.net-mvc ×2
javascript ×2
jquery ×2
ajax ×1
arrays ×1
closedxml ×1
download ×1
file ×1
java ×1
knockout.js ×1
spring-boot ×1
spring-mvc ×1