通过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 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...
随机推荐
- [考试反思]1031csp-s模拟测试95:优势
假的三首杀.因为交文件所以他们都不往OJ上交. 假装是三首杀吧.嗯. 然而昨天还是没有AK.好像说是按照64位评测机的评测结果为准. 但是联赛省选的机子好像都是32位的?也就是和我们正在用的一致. 所 ...
- 「2019.8.9 考试」神仙的dp总让人无所适从
T1是个容斥,我掐手指一算他为了卡容斥的正确性,绝不会把n和m出的很相近($O(n^2)$算法在nm相等的时候达到最高时间复杂度),不然就太好做了,于是开了特判+各种卡常和滚动数组优化,卡到了70分, ...
- 「BZOJ1576」[Usaco2009 Jan] 安全路经Travel------------------------P2934 [USACO09JAN]安全出行Safe Travel
原题地址 题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as ...
- 基本数据类型(While循环,For循环,列表以及相关用法)
正常在没有学习循环情况下,我们要输出同样的语句,需要重复打印.相当重要!!!! While循环 将输出放在一行 end=""默认是换行 print("Hello Worl ...
- html5 点击播放video的方法
html5 点击播放video的方法<pre> <video videosrc="{$vo.shipinurl}" controls="" x ...
- GitHub 发布了官方 App,还打算冰封你的代码一千年
11 月 13 日,GitHub Universe 2019 开发者大会上,公布了大量新功能,包括发布 GitHub 移动版.GitHub Actions 和 Packages 正式版上市.重新设计了 ...
- windows,linux安装redis
windows安装redis Redis介绍 Redis是什么 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string.list ...
- 2019CSP day1t1 格雷码
题目描述 通常,人们习惯将所有 \(n\) 位二进制串按照字典序排列,例如所有 \(2\) 位二进制串按字典序从小到大排列为:\(00,01,11,10\). 格雷码(\(Gray Code\))是一 ...
- ThreadLocal<T> 源码解析
在activeJDBC框架内部的实现中看到了 ThreadLocal 这个类,记录下了每个线程独有的连接 private static final ThreadLocal<HashMap< ...
- Python字符串类型判断错误
Python里面常用的字符串类型有str和unicode,如果要判断一个对象的类型,最好用basestring,否则可能会判断错误: str1 = "hello" str2 = u ...