javascript上传

JScript code

<OBJECT ID=”fso” RUNAT=”Server” PROGID=”Scripting.FileSystemObject” SCOPE=”Page”></OBJECT> <script language=”javascript” runat=”server”> var KBYTE = 1024; var MBYTE = KBYTE * 1024; var GBYTE = MBYTE * 1024; /************************************************* 脚本说明:ASP2JSUpload 最新优惠 器端纯JS上传类 author: wtogether version: 0.1 beta 保留所有权利 任何转载请保留此声明 如果有任何修改,请Email给我一个备份:rkzcmvhkkl@sina.com *************************************************/ //ASP2JSFile文件类 var ASP2JSFile = function() { this.name = arguments[0]; this.fileName = arguments[1]; this.fileExt = arguments[2]; this.fileSize = arguments[3]; this.filePath = arguments[4]; //图片文件的宽度和高度 this.width = arguments[5]; this.height = arguments[6]; //保存临时文件到目标地址 this.saveToFile = function(path) { fso.CopyFile(this.filePath, path); }; }; //ASP2JSUpload类的构造函数 //参数1,最大允许上传字节数 //参数2,当前环境中的字符集 var ASP2JSUpload = function(lMaxSize, pCharset) { this.allowSize = lMaxSize; this.charset = pCharset; this.$form = {}; this.$file = []; this.index = 0; this.stream = new ActiveXObject(“ADODB.Stream”); this.temp = new ActiveXObject(“ADODB.Stream”); this.stream.type = 1; this.stream.open(); }; // 最新优惠 器变量 //参数1,变量名 ASP2JSUpload.prototype.env = function(key) { return “” + Request.ServerVariables(key); } //开始处理上传 //返回1,超过最大数据长度 //返回2,非POST请求模式 //返回3,非multipart/form-data类型数据 //返回4,找不到分界线 //返回0,处理成功 ASP2JSUpload.prototype.start = function() { if (Request.TotalBytes > this.allowSize) return 1; if (this.env(“REQUEST_METHOD”) != “POST”) return 2; if (this.env(“CONTENT_TYPE”).indexOf(“multipart/form-data”) == -1) return 3; var data = this.read(Request.TotalBytes); var key = this.getBoundary(data); if (key == null) return 4; this.startParse(data, key); return 0; }; //读取客户端提交的数据 ASP2JSUpload.prototype.read = function(lSize) { var ret = “”; var lChunk = KBYTE * 200;//设置200KB读取缓存,避免IIS禁止200KB以上的数据读取 var u; while (lSize > lChunk) { this.stream.write(Request.BinaryRead(lChunk)); lSize -= lChunk; } if (lSize > 0) { this.stream.write(Request.BinaryRead(lSize)); } //保存为LOG this.stream.saveToFile(Server.MapPath(“upload.log”), 2); this.stream.position = 0; this.stream.type = 2; //设置UTF-16BE字符集 this.stream.charset = “utf-16be”; while (!this.stream.EOS) { u = this.stream.readText(1).charCodeAt(0); //连接UTF-16BE字符的高低位字节 ret += String.fromCharCode(u >> 8) + String.fromCharCode(u & 0xFF); } //重置stream为adTypeBinary类型 this.stream.position = 0; this.stream.type = 1; return ret; }; //获取分界线 ASP2JSUpload.prototype.getBoundary = function(str) { var pos1 = str.indexOf(“–“); if (pos1 == -1) return null; pos1 += 2; var pos2 = str.indexOf(“\r\n”, pos1); if (pos2 == -1) return null; return str.substring(pos1, pos2); }; //开始解析临时数据 ASP2JSUpload.prototype.startParse = function(data, key) { var pos1, pos2, pos3; var name, filePath, fileName, fileExt, fileSize, width, height; pos1 = data.indexOf(key);//寻找第一个分界线 while (pos1 != -1)//开始循环 { pos1 += key.length + 2;//2 = \r\n的长度 pos2 = data.indexOf(key, pos1);//寻找下一个分界线位置 if (pos2 == -1) break; pos3 = data.indexOf(“\r\n\r\n”, pos1);//寻找报头的结束位置 if (pos3 == -1 || pos3 > pos2) break; pos3 += 4; var mime = this.toString(pos1, pos3 – pos1);//获取报头字符串 if ((name = mime.match(/\sname=\”(.+?)\”/)) == null) break;//寻找name属性位置 name = name[1]; var fileName = mime.match(/\sfilename=\”(.+?)\”/);//寻找filename属性位置 if (fileName)//找到filename属性 { fileName = fileName[1]; fileSize = pos2 – pos3 – 4;//文件长度 if (fileSize > 0 && (fileExt = fileName.match(/\.(\w+)$/)) != null)//获取文件扩展名 { fileExt = fileExt[1]; filePath = Server.MapPath(new Date().getTime() + “.” + this.index++ + “.” + fileExt); //保存为临时文件 this.saveToFile(pos3, fileSize, filePath); this.$file.push(new ASP2JSFile(name, fileName, fileExt, fileSize, filePath, 0, 0)); } else { this.$file.push(new ASP2JSFile(name, fileName, null, fileSize, null, 0, 0)); } } else { var value = this.toString(pos3, pos2 – pos3 – 4);//获取文本元素的元素值 if (this.$form[name]) { this.$form[name].push(value); } else { this.$form[name] = [value]; } } pos1 = pos2;//设置下一个搜索起始位置 } };
« »