简化的ASP无组件上传(主要针对于不喜欢用上传类的程序员)
有人嫌无组件上传类太大(我的上传类是13.8KB大小),下面就写了个简单的单文件上传的示例,顺便把无组件上传的原理也写在代码里面了。下面的示例适合在iframe或者弹出窗口中调用。不过,还是推荐大家用完整的类进行上传,完整的类在功能、易用性以及安全性上都比较好。 http://www.
程序代码:
<form action="?action=upload" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="file1" /> <input type="submit" value="提交" / </form><br /> <% If request.QueryString("action")="upload" Then Call doUpload("upload") End If Function doUpload(Byval savePath) Dim vData, vSplit, SplitStart, SplitEnd, ValueEnd, path, value Dim vdata1, Str, FormNameStart, FormNameEnd, FormName, FileNameStart, FileNameEnd, FileName, ContentType vData = request.BinaryRead(request.TotalBytes) SplitStart = instrb(vData,chrb(13) & chrb(10)) '找到分隔字符串的位置 vSplit = midb(vData, 1, SplitStart - 1) '取出分隔符 SplitEnd = instrb(vData,chrb(13) & chrb(10) & chrb(13) & chrb(10)) '找到文件数据的开始位置 '读出数据前面的描述部分,类似于 'Content-Disposition: form-data; name="file1"; filename="D:\TempFiles\a.jpg" 'Content-Type: image/pjpeg vdata1 = midb(vData,SplitStart + 2,SplitEnd - SplitStart - 2) str = BinToGB(vdata1) FormNameStart = instr(1,str,"name=""") + 6 FormNameEnd = instr(FormNameStart,str,"""") FormName = Mid(str,FormNameStart,FormNameEnd-FormNameStart) '读出表单名字 FileNameStart = instr(1,str,"filename=""")+10 FileNameEnd = instr(FileNameStart,str,"""") FileName = Mid(str,FileNameStart,FileNameEnd-FileNameStart) '读出文件在客户端的路径 FileName = Replace(FileName,"/","\") FileName = split(FileName,"\")(ubound(split(FileName,"\"))) '读出文件名字 SplitEnd = SplitEnd + 4 '数据真正开始的位置 ValueEnd = instrb(SplitEnd,vData,vSplit) '找到数据结束位置 value = midb(vData,SplitEnd,ValueEnd-SplitEnd-2) '取出数据 '保存文件的代码,因为stream直接写入value会出错,因此用adodb.recordset转换一下 Dim tmpStrm Set tmpStrm = server.CreateObject("adodb.stream") tmpStrm.mode=3 tmpStrm.type= 1 tmpStrm.open() Dim Info : Set Info = server.CreateObject("ADODB.Recordset") Info.Fields.Append "value", 205,-1 Info.open() Info.addNew() Info("value").appendChunk(value) tmpStrm.write(Info("value")) Info("value").appendChunk(null) Info.update() Info.Close() Set Info = Nothing tmpStrm.saveToFile server.MapPath(savePath) & "\" & FileName,2 '以原文件名保存到upload下面 tmpStrm.close() Set tmpStrm = Nothing doUpload = FileName End Function '二进制转GB函数 Function BinToGB(Bin) Dim i,tAsc,str for i=1 to lenb(Bin) tAsc = ascb(midb(bin,i,1)) if tAsc<128 Then str = str & chr(tAsc) Else i = i+1 str = str & chr(clng("&H" & hex(tAsc) & hex(ascb(midb(bin,i,1))))) End if Next BinToGB = str End Function %>