通过Javascript 创建POST提交, 无页面刷新下载
前端准备:
//Download the template through "POST" request
function getTargertContainer() {
var $tagertContainer = $("#gridviewContainer");
if ($("#gridview_Import_dialog").size()) {
$tagertContainer = $("#gridview_Import_dialog").parent();
}
return $tagertContainer;
}
function downloadExcelTemplate() {
var criteria = $("#gridview").data("kendoGrid").options.temp.criteria;
criteria.GetIdCollectionOnlyAndIgnorePagination = true;
criteria.withData = true; if ($("#iframeforDownload").size()) {
$("#iframeforDownload").remove();
}
$("<iframe frameborder='1' width='0' height='0' src='about:blank' scrolling='no' id='iframeforDownload'>").appendTo("body");
var iframe = $("#iframeforDownload");
var iframeDocument = iframe[0].contentDocument || iframe[0].contentWindow.document;
var content = "<html><head><meta charset='utf-8' /></head><body><form action='/ExcelBulkEdit/DownloadTemplate' method='post'>";
for (var prop in criteria) {
if (typeof criteria[prop] == "object") {
for (var i = 0; i < criteria[prop].length; i++) {
for (var childprop in criteria[prop][i]) {
content += "<input name='" + prop + "[" + i + "]." + childprop + "' type='hidden' value='" + criteria[prop][i][childprop] + "'/>";
}
}
} else {
content += "<input name='" + prop + "' type='hidden' value='" + criteria[prop] + "'/>";
}
}
content += "</form></body></html>";
if (iframe[0].contentWindow.contents) {
iframe[0].contentWindow.contents = content;
} else {
iframe[0].contentWindow.document.open();
iframe[0].contentWindow.document.write(content);
iframe[0].contentWindow.document.close();
} $(iframeDocument).find("form").submit(); var maxTimeout = 5 * 60 * 1000; //Waiting 5 minutes
var ticks = 0;
var checkTimer = setInterval(function () {
var lowerCaseCookie = "fileDownload=true".toLowerCase();
if (document.cookie.toLowerCase().indexOf(lowerCaseCookie) > -1) {
clearInterval(checkTimer); //execute specified callback
getTargertContainer().stopProgressTimer(); //remove cookie
var cookieData = "fileDownload=; path=/; expires=" + new Date(0).toUTCString() + ";";
document.cookie = cookieData;
return;
}
if (ticks * 1000 > maxTimeout) {
clearInterval(checkTimer);
}
ticks++;
}, 1000);
}
后端准备:
public partial class ExcelBulkEditController : Controller
{
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post), FileDownload]
public void DownloadTemplate(GridQueryCriteriaModel criteria, MetaType metaType, bool withData)
...........
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class FileDownloadAttribute : ActionFilterAttribute
{
public FileDownloadAttribute(string cookieName = "fileDownload", string cookiePath = "/") {
CookieName = cookieName;
CookiePath = cookiePath;
} public string CookieName { get; set; } public string CookiePath { get; set; } /// <summary>
/// If the current response is a FileResult (an MVC base class for files) then write a
/// cookie to inform jquery.fileDownload that a successful file download has occured
/// </summary>
/// <param name="filterContext"></param>
private void CheckAndHandleFileResult(ActionExecutedContext filterContext) {
var httpContext = filterContext.HttpContext;
var response = httpContext.Response; if ( filterContext.Result is EmptyResult )
//jquery.fileDownload uses this cookie to determine that a file download has completed successfully
response.AppendCookie(new HttpCookie(CookieName, "true") { Path = CookiePath });
else
//ensure that the cookie is removed in case someone did a file download without using jquery.fileDownload
if ( httpContext.Request.Cookies[CookieName] != null ) {
response.AppendCookie(new HttpCookie(CookieName, "true") { Expires = DateTime.Now.AddYears(-), Path = CookiePath });
}
} public override void OnActionExecuted(ActionExecutedContext filterContext) {
CheckAndHandleFileResult(filterContext); base.OnActionExecuted(filterContext);
}
}
通过Javascript 创建POST提交, 无页面刷新下载的更多相关文章
- javascript refresh page 几种页面刷新的方法
Javascript刷新页面的几种方法:1 history.go(0) 2 location.reload() 3 location=location 4 location.a ...
- djano modles values+ajax实现无页面刷新更新数据
做项目的过程中想通过不刷新页面的方式来进行页面数据刷新,开始使用http://www.cnblogs.com/ianduin/p/7761400.html方式将查询结果数据进行序列化.发现可以行,但是 ...
- laravel 框架 ajax无页面刷新删除
....................HTML页面<!doctype html><html lang="en"><head> <meta ...
- [javascript library]使用js实现页面刷新后依然保留表单填写的数据
详情请见于该链接:http://sisyphus-js.herokuapp.com/
- javascript页面刷新的几种方法
javascript refresh page 几种页面刷新的方法 本节内容:Javascript刷新当前页面的方法与实例. window.location.reload(),window.histo ...
- JavaScript页面刷新与弹出窗口问题的解决方法
1. [代码][JavaScript]代码 一.无提示刷新网页 大家有没有发现,有些网页,刷新的时候,会弹出一个提示窗口,点“确定”才会刷新.而有的页面不会提示,不弹出提示窗口,直接就刷新了 ...
- Form提交是会刷新页面的
今天发现如果页面中有form,点击提交按钮是会刷新页面的,为了禁止页面刷新行为,可以这么做: <form class="form-horizontal" id="u ...
- ASP.Net中防止页面刷新重复提交的几种方法
[摘要] 目前很多网站都要提交页面插入或更新数据库,比如留言本,一个用户提交留言后,如果按F5,就会重新提交一遍留言,导致数据库出现两条一模一样的留言,本文介绍了几种防止页面刷新,导致重复提交数据的方 ...
- WebForm中如何防止页面刷新,后退导致的重复提交
当用户按下浏览器中的 F5 键刷新当前页面时,对这一过程进行检测所需的操作步骤.页面刷新是浏览器对特定用户操作(按 F5 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...
随机推荐
- [考试反思]1024csp-s模拟测试85:以为
愈发垃圾. T1基本全场切(除了RP<-inf的zkt和把人擦) 然后T2想了半天逐渐趋近于正解,但是因为数据有问题锅了25分,没什么好说的.T3连题意转化都没有完成.括号匹配转为+1/-1做法 ...
- Asp.net Core 系列之--1.事件驱动初探:简单事件总线实现(SimpleEventBus)
ChuanGoing 2019-08-06 前言 开篇之前,简单说明下随笔原因.在园子里游荡了好久,期间也起过要写一些关于.NET的随笔,因各种原因未能付诸实现. 前段时间拜读daxnet的系列文章 ...
- Nginx 的进程结构,你明白吗?
Nginx 进程结构 这篇文章我们来看下 Nginx 的进程结构,Nginx 其实有两种进程结构: 单进程结构 多进程结构 单进程结构实际上不适用于生产环境,只适合我们做开发调试使用.因为在生产环境中 ...
- 使用Samba服务实现Linux与Windows系统之间的文件共享
使用Samba服务口令验证方式可以让共享文件更加的安全,做到仅让信任的用户访问,而且验证过程也很简单,要想使用口令验证模式,我们需要先需要创建Samba服务独立的数据库. 第一步:安装Samba服务软 ...
- canvas线条实践之运动的正方形
原理说明: 1.通过rect实现正方形的绘制: 2.save保存canvas面板的保存,restore回复保存的canvas面板到初始状态: 3.translate用于改变canvas坐标的起始位置: ...
- jquery写$ document.getElementById效果
jquery写$ document.getElementById效果<pre>document.getElementById('video-canvas')和$('#video-canva ...
- java基础阶段几个面试题
1.说出你对面向对象的理解 在我理解,面向对象是向现实世界模型的自然延伸,这是一种“万物皆对象”的编程思想.在现实生活中的任何物体都可以归为一类事物,而每一个个体都是一类事物的实例.面向对象的编程是以 ...
- 一分钟带你了解下MyBatis的动态SQL!
MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格.列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下. 一. ...
- Ubuntu 18 安装搜狗输入法
Ubuntu 18 安装搜狗输入法: 1. 搜狗输入法官网下载对应的Linux输入法 2. 双击 刚刚下载好的 deb 文件 3. 点击 install(安装) 4. 在 settings(系统设置) ...
- nyoj 94-cigarettes (分清楚,那一部分的cigarettes是用过的,那一部分是没有用过的)
94-cigarettes 内存限制:64MB 时间限制:3000ms 特判: No 通过数:13 提交数:20 难度:2 题目描述: Tom has many cigarettes. We hypo ...