通过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 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...
随机推荐
- Spring Boot 2.x实战之StateMachine
本文首发于个人网站:Spring Boot 2.x实战之StateMachine Spring StateMachine是一个状态机框架,在Spring框架项目中,开发者可以通过简单的配置就能获得一个 ...
- Project Euler 56: Powerful digit sum
一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...
- vue.config.js常用配置
使用vue-cli3.0搭建项目比之前更简洁,没有了build和config文件夹. vue-cli3的一些服务配置都迁移到CLI Service里面了,对于一些基础配置和一些扩展配置需要在根目录新建 ...
- python经典算法题目:找出这两个有序数组的中位数
题目:找出这两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以 ...
- 关于swoole 定时器有时候无法清除的解决方法
关于swoole 定时器有时候无法清除的解决方法 有时候start里面写个定时器 有时候你关闭进程的时候 发现定时器还是可以进行 目前只有重启服务器才可以 清除 还有就是ps -ef | grep p ...
- javascript JSMpeg.js 播放视频解决不用全屏也能播放(也支持自动播放哦)
javascript JSMpeg.js 播放视频解决不用全屏也能播放(也支持自动播放哦) 缺陷就是 因为采用的是 MPEG1解码器 所以清晰度有点低 做直播可以考虑下 如果要清晰度高点 可以采取序列 ...
- keeplived离线安装openssl-devel依赖包
转载自素文宅博客:https://blog.yoodb.com/yoodb/article/detail/1434 由于公司业务并发比较高需要高可用使用LVS keeplived.在linux系统ce ...
- count的一些用法
count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL count(1)包括了所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULL count(列名)只包 ...
- 关于laravel框架Model返回的值为stdClass对象转换两种方法
一般情况下laravel模型层查询出来的数据是stdClass对象,无法直接当做数组进行视图展示,所以需要转换为数组格式. Model中查到的数据为 $data ,对它进行转化,转化为数组. 第一 ...
- Netty学习篇⑤--编、解码
前言 学习Netty也有一段时间了,Netty作为一个高性能的异步框架,很多RPC框架也运用到了Netty中的知识,在rpc框架中丰富的数据协议及编解码可以让使用者更加青睐: Netty支持丰富的编解 ...