<?xml version="1.0" encoding="utf-8"?>
<package>
<?component error="true" debug="true"?>
<comment>
<![CDATA[
	上传组件
]]></comment>
<scriptlet id="Upload">
<registration progid="WWS.Upload" version="3.1" description="上传组件" remotable="true" />
<public>
	<property name="Name" dispid="0"><get internalName="ProductName" /></property>
	<property name="Filter" />
	<property name="MaxSize" />
	<property name="Field" />
	<property name="Charset" />
	<method name="ReadForm">
		<parameter name="ReadSize" />
		<parameter name="ProgID" />
	</method>
	<method name="SaveFile">
		<parameter name="SavePath" />
	</method>
	<method name="AspTest">
		<parameter name="Sender" />
	</method>
	<method name="Form">
		<parameter name="Element" />
	</method>
	<method name="Dispose" />
</public>
<implements type="ASP" />
<script language="Javascript">
<![CDATA[
var AspOnly = "本组件仅用于ASP页面。";
var Charset = "UTF-8";
var Rule, Field, Stream, Nothing;
var GetFileBody = MaxSize = 0;
var TempField = LastData = "";
var Filter = "rar|zip|txt|gif|jpg|bmp|htm|html|swf|mht";
function ProductName()
{
	return "WWS上传组件";
}
function ReadForm(ReadSize, ProgID)
{
	Field = new Object;
	if(!this.Server) return AspOnly;
	Rule = Request.ServerVariables("Http_Content_Type").Item;
	if(!Rule || Rule.indexOf("multipart/form-data")) return TextForm();
	Rule = "--" + Rule.split("=")[1];
	Rule = Rule.replace(/[\x00-\xff]/g,
		function($1)
		{
			return "%" + $1.charCodeAt(0).toString(16).toUpperCase();
		});
	var TotalBytes = Request.TotalBytes;
	if(ReadSize % 2) ReadSize += 1;
	if(!ReadSize || ReadSize > TotalBytes) ReadSize = TotalBytes;
	var ReadBytes = 0;
	if(!Stream)Stream = Server.CreateObject("Adodb.Stream");
	while(ReadBytes < TotalBytes)
	{
		ReadBytes += ReadSize;
		if(ReadBytes > TotalBytes)
		{
			ReadSize = ReadSize + TotalBytes - ReadBytes;
			ReadBytes = TotalBytes;
		}
		if(ProgID)
		{
			Application.Lock();
			Application.Contents(ProgID + "_TotalBytes") = TotalBytes;
			Application.Contents(ProgID + "_ReadBytes") = ReadBytes;
			Application.Unlock();
		}
		LastData += EncodeChar(BinToStr(Request.BinaryRead(ReadSize)));
		if(LastData.length < Rule.length) continue;
		if(!GetFileBody && LastData.indexOf(Rule) > -1)
		{
			GetFullData();
		}
		else
		{
			if(!GetFileBody && LastData.indexOf("%0D%0A%0D%0A") > 0)
			{

				var fieldBody = unescape(LastData.replace(/%(\w{2})%(\w{2})/g, "%u$2$1"));
				fieldBody = BinToStr(fieldBody, Charset).split("\r\n\r\n")[0];
				var filePath = fieldBody.match(/ filename="(.*?)"/);
				if(filePath && filePath[1])
				{
					TempField = fieldBody.match(/ name="(.*?)"/)[1];
					Field[TempField] = new FormField;
					Field[TempField].FileType = fieldBody.match(/Type: (.*)$/)[1];
					Field[TempField].FilePath = filePath[1];
					Field[TempField].SourceName = filePath[1].match(/([^\/\\]*?)$/)[1];
					Field[TempField].FileSize = 0;
					LastData = LastData.replace(/^(.*?)%0D%0A%0D%0A/,"");
					Field[TempField].Value = new Array;
					GetFileBody = 1;
				}
			}
			else if(GetFileBody)
			{
				if(LastData.length < Rule.length + 6) continue;
				if(LastData.indexOf("%0D%0A" + Rule) < 0)
				{
					var uniChar = LastData.slice(0, 0 - Rule.length - 6);
					if(uniChar.length % 6) uniChar = uniChar.slice(0, -3);
					Field[TempField].FileSize += uniChar.length;
					if(!MaxSize || Field[TempField].FileSize/3 <= MaxSize)
						Field[TempField].Value.push(unescape(uniChar.replace(/%(\w{2})%(\w{2})/g,"%u$2$1")));
					LastData = LastData.replace(uniChar, "");
				}
				else
				{
					var uniChar = LastData.split("%0D%0A" + Rule)[0];
					Field[TempField].FileSize += uniChar.length;
					LastData = LastData.replace(uniChar, "");
					if(!MaxSize || Field[TempField].FileSize/3 <= MaxSize)
					{
						Field[TempField].Value.push(unescape(uniChar.replace(/%(\w{2})%(\w{2})/g,"%u$2$1")));
						Field[TempField].Value = Field[TempField].Value.join("");
					}
					else
					{
						Field[TempField].Value = "文件太大";
					}
					Field[TempField].FileSize = Field[TempField].FileSize / 3;
					GetFileBody = 0;
					if(LastData.length > Rule.length && ReadBytes == TotalBytes) GetFullData();
				}
			}
		}
	}
}
function SaveFile(SavePath)
{
	if(!this.Server) return AspOnly;
	SavePath = SavePath?SavePath.replace(/[\/\\]$/,""):".";
	var upFilter = new RegExp("\\.(" + Filter + ")$","i");
	for(var i in Field)
	{
		if(Field[i].FilePath)
		{
			if(!Field[i].Value)
			{
				Field[i].Message = "文件内容不能为空";
				continue;
			}
			if(Filter && !Field[i].FilePath.match(upFilter))
			{
				Field[i].Message = "服务器不接受该类文档";
				continue;
			}
			if(MaxSize && Field[i].FileSize > MaxSize)
			{
				Field[i].Message = "上传文档大小超过限制";
				continue;
			}
			var fileExt = Field[i].FilePath.match(/\.([^\/\.\\]*)$/);
			Stream.Type = 2;
			Stream.Open();
			Stream.Charset = "unicode";
			Stream.WriteText(Field[i].Value);
			var filename = MakeFileName() + (fileExt?fileExt[0]:"");
			Field[i].FileName = filename;
			filename = Server.MapPath(SavePath + "\\" + filename);
			Stream.SaveToFile(filename);
			Stream.Close();
			Stream.Type = 1;
			Stream.Open();
			Stream.LoadFromFile(filename);
			Stream.Position = 2;
			var file = Stream.Read(Field[i].FileSize);
			Stream.Position = 0;
			Stream.SetEOS();
			Stream.Write(file);
			Stream.SaveToFile(filename, 2);
			Stream.Close();
		}
	}	
}
function GetFullData()
{
	LastData = LastData.split(Rule);
	for(var C=0; C<LastData.length-1; C++)
	{
		if(LastData[C].length < 10) continue;
		var strField = ParseData(LastData[C]);
		Field[strField[0]] = new FormField;
		Field[strField[0]].Value = strField[1];
		if(strField[2])
		{
			Field[strField[0]].FilePath = strField[2];
			Field[strField[0]].SourceName = strField[2].match(/([^\/\\]*?)$/)[1];
			Field[strField[0]].FileType = strField[3];
			var fileInfo = GetFile(LastData[C]);
			Field[strField[0]].Value = fileInfo[0];
			Field[strField[0]].FileSize = fileInfo[1];
		}
	}
	LastData = LastData.pop();
}
function BinToStr(uniChar, encoding)
{
	Stream.Open();
	Stream.Charset = "unicode";
	Stream.WriteText(uniChar);
	Stream.Position = 0;
	if(encoding && encoding != "unicode") Stream.Charset = encoding;
	uniChar = Stream.ReadText();
	Stream.Close();
	return uniChar;
}
function EncodeChar(uniChar)
{
	uniChar = uniChar.replace(/[\x00-\xff]/g,
		function($1)
		{
			$1=$1.charCodeAt(0).toString(16).toUpperCase();
			if($1.length < 2) $1 = "0" + $1;
			return "_00" + $1;
		});
	return escape(uniChar).replace(/_/g,"%u").replace(/%u(\w{2})(\w{2})/g,"%$2%$1");
}
function ParseData(uniChar)
{
	var fieldBody, fieldName, fieldValue, filePath, mimeType;
	uniChar = unescape(uniChar.replace(/%(\w{2})%(\w{2})/g, "%u$2$1"));
	uniChar = BinToStr(uniChar, Charset);
	fieldBody = uniChar.split("\r\n\r\n");
	fieldName = fieldBody[0].match(/ name="(.*?)"/)[1];
	filePath = fieldBody[0].match(/ filename="(.*?)"/);
	if(filePath)
	{
		filePath = filePath[1];
		mimeType = fieldBody[0].match(/Type: (.*)$/)[1];
	}
	fieldBody.shift();
	fieldValue = fieldBody.join("\r\n\r\n").replace(/\r\n(\x00)?$/,"");
	return [fieldName, fieldValue, filePath, mimeType];
}
function GetFile(uniChar)
{
	uniChar = uniChar.slice(0,-6).replace(/(.*?)(%0D%0A){2}/,"");
	var fileSize = uniChar.length / 3;
	uniChar = unescape(uniChar.replace(/%(\w{2})%(\w{2})/g,"%u$2$1"));
	return [uniChar, fileSize];
}
function MakeFileName()
{
	var time = new Date;
	var arrName = new Array;
	arrName.push(time.getYear().toString().substr(2,2));
	arrName.push(time.getMonth()+1);
	arrName.push(time.getDate());
	arrName.push(time.getHours());
	arrName.push(time.getMinutes());
	arrName.push(time.getSeconds());
	arrName.push(Math.random().toString().substr(2,4));
	return arrName.join("!").replace(/!(\d+)/g,function($0,$1){return $1>9?$1:"0"+$1});
}
function TextForm()
{
	TempField = new Enumerator(Request.QueryString);
	while(!TempField.atEnd())
	{
		Field[TempField.item()] = new FormField;
		Field[TempField.item()].Value = Request.QueryString(TempField.item()).Item;
		TempField.moveNext();
	}
	TempField = new Enumerator(Request.Form);
	while(!TempField.atEnd())
	{
		Field[TempField.item()] = new FormField;
		Field[TempField.item()].Value = Request.Form(TempField.item()).Item;
		TempField.moveNext();
	}
}
function FormField()
{
	this.FileName = Nothing;
	this.FileType = Nothing;
	this.FilePath = Nothing;
	this.FileSize = Nothing;
	this.SourceName = Nothing;
	this.Message = Nothing;
	this.Value = Nothing;
}
function Form(Element)
{
	return (Field[Element] && Field[Element].Value)?Field[Element].Value:Nothing;
}
function AspTest(Sender)
{
	if(!this.Server && Sender.Server)
	{
		this.Server = Sender.Server;
		this.Request = Sender.Request;
		this.Application = Sender.Application;
	}
}
function Dispose()
{
	Field = Stream = Nothing;
	CollectGarbage();
}
]]>
</script>
</scriptlet>
</package>