using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;
/// <summary>
/// Cs裏JAVASCRIPT控制
/// </summary>
/// Author: 姜輝
/// History:
/// 2007-02-08 姜輝 [創建]
namespace UpFile
{
public class Utility : Page
{
/// <summary>
/// 彈出提示
/// </summary>
/// <param name="message"></param>
protected void MsgBox(string message)
{
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = this.Page.ClientScript;
// Define the name and type of the client scripts on the page.
String csname = "PopupScript";
Type cstype = this.GetType();
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname))
{
String cstext = "alert('" + Server.HtmlEncode(message) + "');";
cs.RegisterStartupScript(cstype, csname, cstext, true);
}
}
/// <summary>
/// 彈出提示並轉向url
/// </summary>
/// <param name="message"></param>
/// <param name="url"></param>
protected void MsgBox(string message, string url)
{
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = this.Page.ClientScript;
// Define the name and type of the client scripts on the page.
String csname = "PopupScript";
Type cstype = this.GetType();
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext = "alert('" + Server.HtmlEncode(message) + "');window.location='" + url + "';";
cs.RegisterStartupScript(cstype, csname, cstext, true);
}
}
/// <summary>
/// 最大化打開窗口
/// </summary>
/// <param name="pageName"></param>
public static void COpenMax(string pageName)
{
//Get a ClientScriptManager reference from the Page class
ClientScriptManager cs = this.Page.ClientScript;
//Define the name and type of the client scripts on the page
String csname = "open";
Type cstype = this.GetType();
//builder the script text
StringBuilder cstext = new StringBuilder();
cstext.Append("<script language='javascript'>");
cstext.Append("{window.open('" + pageName + "','aa','width='+screen.width+' height='+screen.height+' top=0 left=0 toolbar=no menubar=no resizable=yes status=yes');}");
cstext.Append("</script>");
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname))
{
cs.RegisterStartupScript(cstype, csname, cstext.ToString(), true);
}
}
/// <summary>
/// 全屏窗口
/// </summary>
/// <param name="pageName"></param>
public static void COpenFullScreen(string pageName)
{
//Get a ClientScriptManager reference from the Page class
ClientScriptManager cs = this.Page.ClientScript;
//Define the name and Type of the client scripts on the page
String csname = "open";
Type cstype = this.GetType();
//builder the script text
StringBuilder cstext = new StringBuilder();
cstext.Append("<script language='javascript'>");
cstext.Append("{window.open('" + pageName + "','aa','fullscreen');}");
cstext.Append("</script>");
// Check to see if the startup script is already registered
if (!cs.IsClientScriptBlockRegistered(cstype, csname))
{
cs.RegisterStartupScript(cstype, csname, cstext.ToString(), true);
}
}
/// <summary>
/// 打開最大化模式窗口
/// </summary>
/// <param name="pageName"></param>
public static void COpenModalDialog(string pageName)
{
//Get a ClientScriptManager reference from the Page class
ClientScriptManager cs = this.Page.ClientScript;
//Define the name and Type of the client script on the page
String csname = "showModalDialog";
Type cstype = this.GetType();
//builder the script text
StringBuilder cstext = new StringBuilder();
cstext.Append("<script language='javascript'>");
cstext.Append("{showModalDialog('" + pageName + "','aa','dialogWidth:'+screen.width+'; dialogHeight:'+screen.height+'; dialogTop:0; dialogLeft:0; center:yes; help:no; resizable:yes; status=yes');}");
cstext.Append("</script>");
//Check to see if the startup script is already registered
if (!cs.IsClientScriptBlockRegistered(cstype, csname))
{
cs.RegisterStartupScript(cstype, csname, cstext.ToString(), true);
}
}
/// <summary>
/// 打開規定大小的模式窗口
/// </summary>
/// <param name="MyPage"></param>
/// <param name="pageName"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public static void COpenModalDialogSelf(string pageName, int width, int height)
{
//Get a ClientScriptManager reference from the Page class
ClientScriptManager cs = this.Page.ClientScript;
//Define the name and Type of the client script on the page
String csname = "showModalDialog";
Type cstype = this.GetType();
//builder the script
StringBuilder cstext = new StringBuilder();
cstext.Append("<script language='javascript'>");
cstext.Append("{showModalDialog('" + pageName + "','aa','dialogWidth:'" + width.ToString() + "'; dialogHeight:'" + height.ToString() + "'; dialogTop:0; dialogLeft:0; center:yes; help:no; resizable:yes; status=yes');}");
cstext.Append("</script>");
//Check to see if the startup script is already registered
if (!cs.IsClientScriptBlockRegistered(cstype, csname))
{
cs.RegisterStartupScript(cstype, csname, cstext.ToString(), true);
}
}
/// <summary>
/// 關閉窗口
/// </summary>
public static void CClose()
{
//Get a ClientScriptManager reference from the Page class
ClientScriptManager cs = this.Page.ClientScript;
//Define the name and Type of the client script on the page
String csname = "close";
Type cstype = this.GetType();
// builder the script text
StringBuilder cstext = new StringBuilder();
cstext.Append("<script language='javascript'>");
cstext.Append("{window.opener='anyone';window.close();}");
cstext.Append("</script>");
//Check to see if the startup script is already registered
if (!cs.IsClientScriptBlockRegistered(cstype, csname))
{
cs.RegisterStartupScript(cstype, csname, cstext.ToString(), true);
}
}
/// <summary>
/// 下載文件
/// </summary>
/// <param name="DstrFileName"></param>
/// <param name="YstrUrl"></param>
/// <returns></returns>
public static bool DownLoadFile(string DstrFileName, string YstrUrl)
{
//打開上次下載的文件或新建文件
long lStartPos = 0;
System.IO.FileStream fs;
if (System.IO.File.Exists(DstrFileName))
{
fs = System.IO.File.OpenWrite(DstrFileName);
lStartPos = fs.Length;
fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移動文件流中的當前指針
}
else
{
fs = new System.IO.FileStream(DstrFileName, System.IO.FileMode.Create);
lStartPos = 0;
}
//打開網絡連接
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(YstrUrl);
if (lStartPos > 0)
request.AddRange((int)lStartPos); //設置Range值
//向服務器請求,獲得服務器迴應數據流
System.IO.Stream ns = request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[512];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, 512);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, 512);
}
fs.Close();
ns.Close();
//("下載完成");
return true;
}
catch
{
fs.Close();
return false;
}
}
}
}