巧用JS實現ASP上傳組件功能

www.8779129.live 原創 歡迎轉載
htmlfile能做神馬?Google用它來做長連接,我們還能用它來做ASP上傳 。
下面的代碼僅創建了一個htmlfile的ActiveX對象,沒做文件保存。
原理是利用了recordset的appendChunk方法來連續讀取上傳數據,解決IIS上傳默認不允許超過200kb的問題,然后使用xml分析二進制內容。
上代碼吧:

<%@ language="javascript" codepage="65001" %><%
Boot();
function Boot()
{
        if(Request.QueryString("Mode").Item != "Down") return ShowForm();
        // 文件下載處理
        if(!Application("TempName")) return Response.Write("No File");
        Response.addHeader("Content-Disposition", "Attachment;filename=\"" + encodeURIComponent(Application("TempName")) + "\"");
        Response.addHeader("Content-Length", Application("TempSize"));
        Response.BinaryWrite(Application("TempFile"));
}
function ShowForm()
{ %><!doctype html><html><head>
        <meta charset="UTF-8" />
        <title>文件上傳</title>
        <style type="text/css">
        body{ font: 4mm/7mm simsun }
        </style>
</head><body><%
var up = new UploadClass;
if(Request.ServerVariables("REQUEST_METHOD").Item == "POST")
{
        var str = up.ReadForm();
        if(str != "OK") Response.Write(str);
        else
        {
                var data = up.Form("test");
                if(data[0].Data) Response.Write("您提交的文本為:" + data[0] + "<br />");
                if(data[1].File)
                {
                        Response.Write("您上傳的文件是:<a href=\"?Mode=Down\">" + data[1].File + "</a> (" + data[1].Size + " bytes)<br />");
                        Application("TempFile") = data[1].Data;
                        Application("TempSize") = data[1].Size;
                        Application("TempName") = data[1].File;
                }
        }
}%><form method="post" enctype="multipart/form-data">
文本:<input name="test" /><br />
文件:<input name="test" type="file" />
<input type="submit" value="上傳" /><br />
當前最大允許上傳 <%= up.MaxSize %> Bytes,支持的文件類型:<%= up.Filter %><%
if(Application("TempName")) Response.Write("<br />當前的臨時文件為:<a href=\"?Mode=Down\">" + Application("TempName") + "</a> (" + Application("TempSize") + " Bytes)");
%>
</form></body></html><% }

/* 文件上傳類,支持多文件及文字混合上傳 */
function UploadClass()
{
        this.MaxSize = 512 * 1024;
        this.Filter = "jpg|png|gif|bmp|doc|docx|txt|zip|rar";
        var data = new Object;        // 存儲表單對象
        this.ReadForm = function()
        {
                var nmbTotal = Request.TotalBytes;
                if(nmbTotal > this.MaxSize) return "文件大小超過限制。";
                var strRule = Request.ServerVariables("Http_Content_Type").Item || "";
                strRule = (strRule.match(/boundary=(.+)/) || [])[1];
                if(!strRule) return "非文件上傳表單";
                strRule = "2d2d" + strRule.replace(/[\w\-]/g, function($1){ return $1.charCodeAt(0).toString(16); });
                var xml = new ActiveXObject("htmlfile").createElement("xml");
                var root = xml.createElement("root"); root.dataType = "bin.hex";
                var rcd = xml.recordset; rcd.fields.append("bin", 205, -1);
                rcd.open(); rcd.addNew();
                var nmbByte = 0, nmbRead = 200000;        // 緩沖區大小
                while(Response.IsClientConnected() && nmbByte < nmbTotal)
                {
                        rcd(0).appendChunk(Request.BinaryRead(nmbRead));
                        nmbByte += nmbRead;
                }
                root.nodeTypedValue = rcd(0).value; rcd.close();
                var arr = root.text.split(strRule); arr.shift(); arr.pop();
                var reg = new RegExp("\\.(?:" + this.Filter + ")$", "i");
                for(var i = 0; i < arr.length; i++)
                {
                        var nmbSplit = arr[i].indexOf("0d0a0d0a");
                        // 此處僅支持UTF-8解碼,gbk請借助rcd或流對象
                        var strHead = arr[i].slice(4, nmbSplit).replace(/(\w{2})/g, "%$1");
                        strHead = decodeURIComponent(strHead);
                        var strPath = (strHead.match(/filename="(.+?)"/i) || [])[1] || "";
                        strPath = (strPath.match(/([^\\\/]+)$/) || [])[1];
                        if(strPath && !reg.test(strPath)) return "不允許的文件類型。";
                        // 查找兩個空行,忽略最后一個空行
                        root.text = arr[i].slice(nmbSplit + 8, -4);
                        var field = (strHead.match(/name="(.+?)"/i) || [])[1] || "";
                        if(!data[field]) data[field] = new Array;
                        data[field].push(
                        {
                                File : strPath, Size : root.text.length / 2,
                                Data : strPath ? root.nodeTypedValue : decodeURIComponent(root.text.replace(/(\w{2})/g, "%$1")),
                                toString : function(){ return this.Data; }
                        });
                }
                return "OK";
        }
        this.Form = function(strField)
        {
                var item = data[strField] || [];
                return item.length < 2 ? item[0] : item;
        }
} %>

专注投入 才能赚钱 山西省十一选五开奖 新快三开奖结果走势图 吉林快三是不是官方 时时彩最快开奖软件 众彩网河南11选5走势图 股票跌停当天还能涨 排列五app软件下载 2010年7月上证指数 淘宝天天红包赛登陆不了了 股票行情000980