📄 xu_class.cs
字号:
// ****************************************************
// NET for XUploadFiles 2.1.0.0
// 程序制作:blue999.com
// 版权所有:blue999.com
// 主页地址:http://www.blue999.com/xuploadfiles/
// 程序申明:用于商业应用时请与作者联系
// 最后修改:2005.11.16
// ****************************************************
namespace XUploadFiles{
using System;
using System.Web.UI;
using System.Text.RegularExpressions;
public class XUpload_page : Page{
}
public class XUpload_class{
private System.Web.HttpServerUtility Server;
private System.Web.HttpRequest Request;
private System.Web.HttpResponse Response;
private uint SFNHV;
private bool RYXYV,UVBWH;
private uint ZFSKG,ITLUT,GIONB,TLRKK;
private string XASTU,RRUGY,RXZWA,CANBM;
private char JEGUI;
private bool IXIZC;
private string MJKVP;
public uint GetVersion(){ return SFNHV; }
public bool IsError(){ return RYXYV; }
public void SetOverlayMode(bool value){ UVBWH=value; }
public void SetMinFileSize(uint value){ ZFSKG=value; }
public void SetMaxFileSize(uint value){ ITLUT=value; }
public void SetMaxFileCount(uint value){ GIONB=value; }
public void SetMaxTotalSize(uint value){ TLRKK=value; }
public void SetAllowExt(string value){ XASTU=value.ToLower().Trim(); if(XASTU!="") XASTU=";" + XASTU + ";"; }
public void SetDenyExt(string value){ RRUGY=value.ToLower().Trim(); if(RRUGY!="") RRUGY=";" + RRUGY + ";"; }
public void SetThumbImage(string value){ RXZWA=value.Trim(); }
public void SetStatURL(string value){ CANBM=value.Trim(); }
public bool HasThumbImage(){ return Request.Form["xu_thumbtag"]=="thumb"; }
public void OutErr(string message){ RYXYV=true; Response.Write(message); }
public void Out(string message){ if(!RYXYV){ RYXYV=true; Response.Write("ok"); } Response.Write(JEGUI); Response.Write(message); }
public XUpload_class(object mainpage){
Server=((System.Web.UI.Page)mainpage).Server;
Request=((System.Web.UI.Page)mainpage).Request;
Response=((System.Web.UI.Page)mainpage).Response;
SFNHV=2100;
RYXYV=false;
SetOverlayMode(false);
SetMinFileSize(1);
SetMaxFileSize(10485760);
SetMaxFileCount(100);
SetMaxTotalSize(10485760);
SetAllowExt("");
SetDenyExt("asa;asp;cdx;cer;aspx;php;config;cs;csproje;vb;vbproj;webinfo");
SetThumbImage("");
SetStatURL("");
JEGUI='\x07';
IXIZC=false;
MJKVP=Server.MapPath("_temp") + "\\";
}
public void InitParameters(){
if(RYXYV) return;
if(Convert.ToInt32(Request.Form["xu_version"]) < SFNHV)
OutErr("您的上传插件版本太低,请升级到最新版本。");
else if(Regex.IsMatch(Request.Form["xu_verifier"],@"\D") || Regex.IsMatch(Request.Form["xu_tag"],@"(\/|\\)"))
OutErr("发送的数据格式不正确。");
else if(Request.Form["xu_operate"]=="init"){
if(!System.IO.Directory.Exists(MJKVP)) System.IO.Directory.CreateDirectory(MJKVP);
Out(Convert.ToString(SFNHV));
Out(Convert.ToString(ZFSKG));
Out(Convert.ToString(ITLUT));
Out(Convert.ToString(GIONB));
Out(Convert.ToString(TLRKK));
Out(XASTU);
Out(RRUGY);
Out(RXZWA);
Out(CANBM);
RYXYV=true;
}
else if(Request.Form["xu_operate"]=="postclose"){
string LNTWN=Request.Form["xu_tag"];
if(LNTWN!="" && LNTWN!="0"){
string YUWNZ=MJKVP + Request.Form["xu_verifier"] + LNTWN + ".t";
if(System.IO.File.Exists(YUWNZ)) System.IO.File.Delete(YUWNZ);
YUWNZ=MJKVP + Request.Form["xu_verifier"] + LNTWN + "thumb" + ".t";
if(System.IO.File.Exists(YUWNZ)) System.IO.File.Delete(YUWNZ);
}
Out("ok");
RYXYV=true;
}
}
public bool IsUploadFile(){
string LNTWN;
if(RYXYV) return false;
switch(Request.Form["xu_operate"]){
case "postdata" :
System.Web.HttpPostedFile postfile=Request.Files["xu_postfile"];
LNTWN=Request.Form["xu_tag"];
if(LNTWN=="0" ||
(LNTWN=="" && Request.Form["xu_desc"]!="") ||
(LNTWN!="" && Convert.ToInt32(Request.Form["xu_length"])==0) ||
(LNTWN!="" && Convert.ToInt32(Request.Form["xu_offset"])==0 && Request.Form["xu_desc"]=="") ||
Regex.IsMatch(Request.Form["xu_desc"],@"(\/|\\|\.)") ||
postfile.ContentLength!=Convert.ToInt32(Request.Form["xu_length"]) ||
Convert.ToInt32(Request.Form["xu_offset"]) + Convert.ToInt32(Request.Form["xu_length"]) > Convert.ToInt32(Request.Form["xu_filelength"])){
OutErr("发送的数据格式不正确。");
return false;
}
if(Convert.ToInt32(Request.Form["xu_filelength"]) < ZFSKG){
OutErr("上传文件的长度小于服务器的最小限制。");
return false;
}
if(Convert.ToInt32(Request.Form["xu_filelength"]) > ITLUT){
OutErr("上传文件的长度超过服务器限制。");
return false;
}
if(LNTWN=="" && !NVKWL(postfile.FileName)){
OutErr("服务器不接受该类型的文件。");
return false;
}
string WAMEJ=MJKVP + Request.Form["xu_verifier"] + LNTWN + Request.Form["xu_desc"] + ".t";
if(LNTWN==""){
if(postfile.ContentLength==0){
LNTWN="0";
} else{
Random ra=new Random();
for(int i=0; i < 100; i++){
LNTWN=Convert.ToString(ra.Next());
WAMEJ=MJKVP + Request.Form["xu_verifier"] + LNTWN + Request.Form["xu_desc"] + ".t";
if(!System.IO.File.Exists(WAMEJ)) break;
}
}
}
if(postfile.ContentLength > 0){
if(Convert.ToInt32(Request.Form["xu_offset"]) > 0){
if(!System.IO.File.Exists(WAMEJ)){
OutErr("原始数据被服务器清除了,请重新进行操作。");
return false;
}
System.IO.FileInfo info=new System.IO.FileInfo(WAMEJ);
if(info.Length!=Convert.ToInt64(Request.Form["xu_offset"])){
OutErr("数据参数有问题,不能进行上传。");
return false;
}
}
byte[] filedata=new byte[postfile.ContentLength];
int readlen=postfile.InputStream.Read(filedata,0,postfile.ContentLength);
if(readlen!=Convert.ToInt32(Request.Form["xu_length"])){
OutErr("服务器保存数据出现错误,请重新进行操作。");
return false;
}
System.IO.FileStream targetfile=new System.IO.FileStream(
WAMEJ,
Convert.ToInt32(Request.Form["xu_offset"])==0 ?
System.IO.FileMode.Create :
System.IO.FileMode.Append);
targetfile.Write(filedata,0,readlen);
targetfile.Close();
}
Out(LNTWN);
RYXYV=true;
break;
case "postfile":
break;
default:
OutErr("服务器不支持该命令。");
break;
}
return !RYXYV;
}
public string SaveToFile(string WAMEJ){
if(RYXYV || WAMEJ=="") return "";
if(WAMEJ.IndexOf(':') < 0) WAMEJ=Server.MapPath(WAMEJ);
if(!NVKWL(WAMEJ)){
OutErr("服务器不接受该类型文件。");
return "";
}
if(Convert.ToInt32(Request.Form["xu_filesize"]) < ZFSKG){
OutErr("上传文件的长度小于服务器的最小限制。");
return "";
}
if(Convert.ToInt32(Request.Form["xu_filesize"]) > ITLUT){
OutErr("上传文件的长度超过服务器限制。");
return "";
}
if(!UVBWH){
string QFUVY="";
Random ra=new Random();
int pos=WAMEJ.LastIndexOf('.');
if(pos < 0) pos=WAMEJ.Length;
for(int i=0; i < 100; i++){
QFUVY="-" + Convert.ToString((ra.Next() % 9000) + 1000);
if(!System.IO.File.Exists(WAMEJ.Insert(pos,QFUVY))) break;
}
WAMEJ=WAMEJ.Insert(pos,QFUVY);
}
if(Request.Form["xu_tag"]=="0"){
System.IO.FileStream targetfile=new System.IO.FileStream(WAMEJ,System.IO.FileMode.Create);
targetfile.Close();
} else{
string YUWNZ=MJKVP + Request.Form["xu_verifier"] + Request.Form["xu_tag"] + ".t";
System.IO.FileInfo info=new System.IO.FileInfo(YUWNZ);
if(!System.IO.File.Exists(YUWNZ) || info.Length!=Convert.ToInt64(Request.Form["xu_filesize"])){
OutErr("发送的数据格式不正确。");
return "";
}
if(System.IO.File.Exists(WAMEJ)) System.IO.File.Delete(WAMEJ);
System.IO.File.Move(YUWNZ,WAMEJ);
}
return WAMEJ;
}
public string SaveThumbToFile(string WAMEJ){
if(WAMEJ.IndexOf(':') < 0) WAMEJ=Server.MapPath(WAMEJ);
string YUWNZ=MJKVP + Request.Form["xu_verifier"] + Request.Form["xu_tag"] + Request.Form["xu_thumbtag"] + ".t";
if(RYXYV || WAMEJ=="" ||
!NVKWL(WAMEJ) ||
!HasThumbImage() ||
Request.Form["xu_tag"]=="0" ||
Convert.ToInt32(Request.Form["xu_thumbsize"])==0 || Convert.ToInt32(Request.Form["xu_thumbsize"]) > ITLUT ||
!System.IO.File.Exists(YUWNZ)){
return "";
}
System.IO.FileInfo info=new System.IO.FileInfo(YUWNZ);
if(info.Length!=Convert.ToInt64(Request.Form["xu_thumbsize"])) return "";
if(System.IO.File.Exists(WAMEJ)) System.IO.File.Delete(WAMEJ);
System.IO.File.Move(YUWNZ,WAMEJ);
return WAMEJ;
}
public string CreateFileName(string IYPOT,string WFLBF,string WAMEJ){
WAMEJ=System.IO.Path.GetFileName(WAMEJ);
if(WFLBF.IndexOf('.') >= 0){
OutErr("路径或文件名中包含有非法字符");
return "";
}
if(IYPOT=="") IYPOT=".";
if(IYPOT.IndexOf(':') < 0) IYPOT=Server.MapPath(IYPOT) + "\\";
if(IYPOT!=""){
char ch=IYPOT[IYPOT.Length-1];
if(ch!='\\' && ch!='/') IYPOT += "\\";
}
if(WFLBF!=""){
if(WFLBF[0]=='\\' || WFLBF[0]=='/') WFLBF=WFLBF.Substring(1);
}
if(WFLBF!=""){
char ch=WFLBF[WFLBF.Length-1];
if(ch!='\\' && ch!='/') WFLBF += "\\";
}
return(IYPOT + WFLBF + WAMEJ).Replace('/','\\');
}
public string CreateFileURL(string IYPOT,string WFLBF,string WAMEJ){
WAMEJ=System.IO.Path.GetFileName(WAMEJ);
if(IYPOT=="") IYPOT="./";
if(IYPOT.IndexOf(':') < 0){
char ch=IYPOT[IYPOT.Length-1];
if(ch!='\\' && ch!='/') IYPOT += "/";
} else{
IYPOT="/*内部地址*/";
}
if(WFLBF!=""){
if(WFLBF[0]=='\\' || WFLBF[0]=='/') WFLBF=WFLBF.Substring(1);
}
if(WFLBF!=""){
char ch=WFLBF[WFLBF.Length-1];
if(ch!='\\' && ch!='/') WFLBF += "/";
}
return(IYPOT + WFLBF + WAMEJ).Replace('\\','/');
}
private bool NVKWL(string WAMEJ){
if(WAMEJ.IndexOf(':') < 0) WAMEJ=Server.MapPath(WAMEJ);
if(WAMEJ!=System.IO.Path.GetFullPath(WAMEJ)) return false;
string sExt;
int pos=WAMEJ.LastIndexOf('.');
if(pos >= 0){
sExt=";" + WAMEJ.Substring(pos+1).ToLower().Trim() + ";";
if(XASTU!=""){ if(XASTU.IndexOf(sExt) < 0) return false; }
else if(RRUGY!=""){ if(RRUGY.IndexOf(sExt) >= 0) return false; }
} else if(XASTU!=""){ return false; }
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -