通过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 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...
随机推荐
- CentOS 8 发布了
CentOS 8 的发现注记是: https://wiki.centos.org/Manuals/ReleaseNotes/CentOS8.1905 CentOS 在 2019 年 9 月 25 日 ...
- python 3.7.5 官方tutorial 学习笔记
用了好久python,还没有完整看过官方的tutorial,这几天抽空看了下,还是学到些东西 --- Table of Contents 1. 课前甜点 2. 使用 Python 解释器 2.1. 调 ...
- js动态显示当前时间+数字大小转换+小于9前面补0
<script type="text/javascript"> function getTime(){ var myDate = new Date(); // 年份 d ...
- 面试总问的jvm调优到底是要干什么?
1. 压力测试的理解,xxx的性能10w/s,对你有意义么? 没有那家卖瓜的会说自己家的不甜,同样,没有哪个开源项目愿意告诉你在对它条件最苛刻的时候压力情况是多少,一般官网号称给你看的性能指标都是在最 ...
- My First Cloud Application's Design
Structure Diagram as following: Questions list: 1. Skydrive Integration > Based on the MSDN commu ...
- [javascript] 编写一个计算器,实现加减法
1.代码 <script> function sum(){ //加法 var value1 = document.getElementById("num1").valu ...
- [LC]237题 Delete Node in a Linked List (删除链表中的节点)(链表)
①中文题目 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: hea ...
- 区块链原理、设计与应用pdf电子版下载
链接:https://pan.baidu.com/s/1koShkDjEYOXxLOewZJU2Rw 提取码:8ycx 内容简介 · · · · · · 本书由专业区块链开发者撰写,是区块链开发起步 ...
- Spring-boot构建多模块依赖工程时,maven打包异常:程序包xxx不存在
在qizhi项目改版的时候, 所有代码都迁移好了, 但是compile的时候报程序包*****不存在, 具体到某一个类就是: 找不到符号. 下面这篇文章是正解 http://hbxflihua.ite ...
- 领扣(LeetCode)两个数组的交集II 个人题解
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...