ASP.NET 下载文件并继续执行JS解决方法
需求说明:当用户点击按钮时使当前按钮为不可用,并打开新页面,关闭新页面时,按钮变为可用。并且如果不关闭新页面,当前按钮过10秒钟自动变为可用。
包含3个页面:
一、按钮页
前台代码:当刷新后采用js进行disable后,点击的时候也可以用JS使其为disable,让整个过程都disable。(如果采用的是后台button.Enable=false,则不行,因为服务器端状态始终为false,导致按钮只能用一次。)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script type="text/javascript" language="javascript"> function disableButton(button) {
var btn = document.getElementById(button);
setTimeout(function () {disable_in(btn); },);
}
function disable_in(button)
{
button.disabled=false;
}
function Enablebutton(buttonID) {
var button = document.getElementById(buttonID);
button.disabled = false;
}
function NotEnablebutton(buttonID) {
var button = document.getElementById(buttonID);
button.disabled = true;
} function disable_out(button) {
button.disabled = true;
} // function disableButton_Out(button) {
// setTimeout(function () { disable_out(button); }, 50);
// } </script>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="false" OnClientClick="disable_out(this);"/>
<asp:Button ID="Button2" runat="server" Text="Button2" UseSubmitBehavior="false" OnClientClick="disable_out(this);"/>
</div>
</form>
</body>
</html>
后台代码:
这里用服务器端 Button1.Enabled = False代码, 当用JS还原为可用时,服务器端状态不会改变,因此多个按钮点击时会出现bug(点击第二个按钮时,第一个按钮会一起变为不可用)。所以这里将用js设置为不可用状态。
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Session("ImagePath") = "\\129.223.229.102\Dis_project\Temp\test.pdf"
'Session("ImagePath") = "\\129.223.229.102\Dis_project\Temp\test1.zip"
Session("CurrentButtonID") = Button1.ClientID
Session("IsPDF") = True
System.Threading.Thread.Sleep()
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript1", String.Format("<script>disableButton('" & Button1.ClientID & "')</script>"))
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript2", String.Format("<script>NotEnablebutton('" & Button1.ClientID & "');</script>"))
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", String.Format("<script>window.open('ShowPDF.aspx')</script>"))
'Button1.Attributes.Add("OnClientClick", String.Format("javascript:disableButton_Out(this);"))
'Button1.Enabled = False End Sub
二、下载(显示)页面
前台代码:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ShowPDF.aspx.vb" Inherits="ShowPDF" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ShowPDF</title>
</head>
<body>
<form id="form1" runat="server"> <%--<script type="text/javascript" language="javascript">
window.onbeforeunload = closingCode;
function closingCode() {
opener.Enablebutton();
} </script>--%>
<%--<% GetPDF1()%>--%>
<iframe src="Default3.aspx" width="100%" height="100%">
</iframe>
</form>
</body>
</html>
后台代码:
Imports System.Net
Partial Class ShowPDF
Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim js As String = ""
Dim buttonid As String = ""
If Not Session("ImagePath") Is Nothing AndAlso Not Session("CurrentButtonID") Is Nothing Then
buttonid = Session("CurrentButtonID").ToString()
js = "<script>window.onbeforeunload = closingCode;function closingCode() {opener.Enablebutton('" + buttonid + "');}</script>"
'js = String.Format("<script>window.onbeforeunload = closingCode;function closingCode() {opener.Enablebutton('{0}');}</script>", "Button1")
Response.Write(js) 'write the enable parent button'js
End If End Sub
Public Sub ShowPDFfile()
Dim str As String = ""
If Not Session("ImagePath") Is Nothing Then
str = String.Format("<object data=""\\129.223.229.102\Dis_project\Temp\test.pdf"" type=""application/pdf"" width=""100%"" height=""100%""><p>It appears you don't have a PDF plugin for this browser.No biggie... you can <a href=""em.pdf"">click here todownload the PDF file.</a></p></object>")
End If
Response.Write(str)
End Sub Public Sub GetPDF(ByVal path As String)
'Dim path As String = Server.MapPath(Spath)
Dim client As New WebClient()
Dim buffer As [Byte]() = client.DownloadData(path) If buffer IsNot Nothing Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.Flush()
Response.End()
End If
End Sub
Protected Sub DownloadFile(ByVal fileName As String)
'20110905 Test Export with SSL using IE 7
Dim s_path As String = fileName 'HttpContext.Current.Server.MapPath("~/") +
Dim file As System.IO.FileInfo = New System.IO.FileInfo(s_path) Response.ClearHeaders() HttpContext.Current.Response.ContentType = "application/ms-download"
HttpContext.Current.Response.Charset = "utf-8"
Response.AddHeader("Content-Disposition", "inline; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)) 'inline
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString())
Response.AddHeader("Pragma", "public")
Response.AddHeader("Cache-Control", "max-age=0")
Response.Flush()
HttpContext.Current.Response.WriteFile(file.FullName)
HttpContext.Current.Response.Flush()
'HttpContext.Current.Response.Clear()
HttpContext.Current.Response.End() End Sub
Public Sub GetPDF1()
Dim fileName As String = "\\129.223.229.102\Dis_project\Temp\test1.pdf"
Response.ContentType = "application/pdf"
Response.Clear()
Response.TransmitFile(fileName)
Response.End()
End Sub
End Class
三、框架中的真正的下载(显示页面),显示在框架中
前台代码:
后台代码:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileName As String = ""
If Not IsPostBack Then
If Not Session("ImagePath") Is Nothing AndAlso Not Session("CurrentButtonID") Is Nothing Then
fileName = Session("ImagePath").ToString()
GetPDF1(fileName)
End If
End If
End Sub
Public Sub GetPDF1(ByVal fileName As String)
'Dim fileName As String = "\\129.223.229.102\Dis_project\Temp\test1.pdf"
If Session("IsPDF") = False Then
Response.ContentType = "application/zip"
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
Else
Response.ContentType = "application/pdf"
End If
Response.Clear()
Response.TransmitFile(fileName)
Response.End()
End Sub
ASP.NET 下载文件并继续执行JS解决方法的更多相关文章
- Asp.Net 下载文件的几种方式
asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...
- 自动化测试中执行JS脚本方法封装
执行JS脚本方法封装: class JavaScript(Base): def execute_javascript(self, js): """执行 JavaScrip ...
- XCode编译文件过多导致内存吃紧解决方法
XCode编译文件过多导致内存吃紧解决方法 /Users/~~/Library/Developer/Xcode/DerivedData 1) 然后 找到编译文件 删除 就好了哦 快去试试看吧
- PowerShell因为在此系统中禁止执行脚本解决方法
PowerShell因为在此系统中禁止执行脚本解决方法 在Powershell直接脚本时会出现: 无法加载文件 ******.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 " ...
- (转)调用System.gc没有立即执行的解决方法
调用System.gc没有立即执行的解决方法 查看源码 当我们调用System.gc()的时候,其实并不会马上进行垃圾回收,甚至不一定会执行垃圾回收,查看系统源码可以看到 /** * Indicate ...
- SQL Server作业没有执行的解决方法
SQL Server作业没有执行的解决方法 确保SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行 设置方法: 我的电脑--控制面板--管理工具--服务--右键 SQLSE ...
- WebBrowser 的 DocumentCompleted事件不执行的解决方法
原文:WebBrowser 的 DocumentCompleted事件不执行的解决方法 WebBrowser 的 DocumentCompleted事件不执行的解决方法: 使用WebBrowser的P ...
- asp.net下载文件几种方式
测试时我以字符流的形式下载文件,可行,前几个仅作参考 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Respo ...
- Asp.net下载文件
网站上的文件是临时文件, 浏览器下载完成, 网站需要将其删除. 下面的写法, 文件读写后没关闭, 经常删除失败. /// <summary> /// 下载服务器文件,参数一物理文件路径(含 ...
随机推荐
- XDU 1161 - 科协的数字游戏II
Problem 1161 - 科协的数字游戏II Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 112 ...
- 【C语言入门教程】4.4 指针 与 指针变量
在程序中声明变量后,编译器就会为该变量分配相应的内存单元.也就是说,每个变量在内存会有固定的位置,有具体的地址.由于变量的数据类型不同,它所占的内存单元数也不相同.如下列声明了一些变量和数组. int ...
- PHPCMS调用form类编辑器editor函数动态上传图片附件
http://w3note.com/web/49.html phpcms v9的系统类库有一个表单类,它封装了表单的一些组件,如编辑器.图片上传.时间选择器.模板选 择器等,更详细请参考form.cl ...
- windows和linux文件共享
###Samba安装 [root@samba ~]# yum install -y samba* [root@samba ~]# rpm -qa | grep samba ###开启s ...
- Java多线程基础知识(五)
一. Java中的13个原子操作类 在Jdk1.5中,这个包中的原子操作类提供了一种用法简单,性能高效,线程安全的更新一个变量的方式. 1. 原子更新基本类型类 AtomicBoolean : 原子更 ...
- HDU 4803 Poor Warehouse Keeper
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4803 解题报告:有一个记录器,一共有两个按钮,还有两行屏幕显示,第一行的屏幕显示的是数目,第二行的屏幕 ...
- 跟着百度学PHP[4]OOP面对对象编程-6-封装性private
所谓封装顾名思义,如同箱子般给封装起来.结合前面的来说就是对属性或者方法,封装后的方法或属性只能有类内部进行调用.外部调用不了. 封装性的好处: 1.信息隐藏 2.http://www.cnblogs ...
- Sqli-LABS通关笔录-9[延时注入]
通过这个关卡 1.更快的掌握到了如何判断是否是延时注入 无论咋输入,都不行.当payload为: http://127.0.0.1/sql/Less-9/index.php?id=1' and sle ...
- 如何安装PANABIT?
PANABIT可以帮你轻松的封杀360WIFI.二级路由和移动设备,还可以让员工在上班期间禁止浏览与工作无关的网站,禁止看视频,禁止聊QQ,禁止网购......总之一切你能想到的,它都能帮你实现. P ...
- 1. Smalidea无源码调试android应用
一.安装smalidea https://github.com/JesusFreke/smali/wiki/smalidea 1. 进入IntelliJ IDEA/Android Studio开始 ...