通过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 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...
随机推荐
- JAVA内存溢出与内存泄露
虽然jvm可以通过GC自动回收无用的内存,但是代码不好的话仍然存在内存溢出的风险. 最近在网上搜集了一些资料,现整理如下: —————————————————————————————————————— ...
- 洛谷 pP2146 [NOI2015]软件包管理器
题目的传送门 题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...
- 基于 HTML5 + WebGL 实现 3D 挖掘机系统
前言 在工业互联网以及物联网的影响下,人们对于机械的管理,机械的可视化,机械的操作可视化提出了更高的要求.如何在一个系统中完整的显示机械的运行情况,机械的运行轨迹,或者机械的机械动作显得尤为的重要,因 ...
- 基于代码生成器的快速开发平台 JEECG
JEECG是一款基于代码生成器的J2EE快速开发平台,开源界“小普元”超越传统商业企业级开发平台.引领新的开发模式(Online Coding模式(在线开发)->代码生成器模式->手工ME ...
- Opencv-Python项目(1) | 基于meanshiftT算法的运动目标跟踪技术学习
目标跟踪(object tracking)就是在连续的视频序列中,建立所要跟踪物体的位置关系,得到物体完整的运动轨迹. 目标跟踪分为单目标跟踪和多目标跟踪.本文如无特别指出,均指单目标跟踪. 通常的做 ...
- css3关于body的默认滑动机制
css关于body的默认滑动机制 大家都知道 body里面只要高度超出了原来的高度就可以滚动要取消这个机制 只能设置height:100% overflow:hidden就能取消了
- IO类
Java的IO体系分为Input/Output和Reader/Writer两类,区别在于Reader/Writer在读写文本时能自动转换内码.基本上,所有的IO类多是配对的,即有XXXInput,就有 ...
- containerd 与安全沙箱的 Kubernetes 初体验
作者 | 易立 阿里云资深技术专家 containerd 是一个开源的行业标准容器运行时,关注于简单.稳定和可移植,同时支持 Linux 和 Windows. 2016 年 12 月 14 日,Do ...
- jenkins里的定时构建
1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月的第几天,取值1~31第四个*表示第几月,取 ...
- 微信小程序this.data和this.setData({})的区别
this.data.xx是用来获取页面data对象的----------只是js(逻辑层)数据的更改: this.setData是用来更新界面的---------用于更新view层的.