数据读取中...
 您当前位置:惠州维修 -> 网络-> asp技术交流 文章搜索:  
ASP无组件上传·从原理剖析到实践五
作者:转载 来源:惠州维修
日期: 2004-10-6
放大字体显示 缩小字体显示 打印文章 推荐给朋友
============================================================== 
第七天:实现附加功能  

今天,我们就来实现昨天提出的方法和属性,来完善我们的文件上传类。以前没有太注意的性能问题,这一次也要彻底的解决: 
1。所有的变量先声明,后使用; 
2。设置类的teminate方法; 
3。简化有些地方的写法,注意细节。 
我们的原则,就是先实现,后优化。当然,象变量声明这样的东西,如果程序很大,最好还是在写程序的时候一次过。如果写完了才加,可以在页面开头加上option explicit(强制变量声明),然后测试所有的方法和属性,直到没有错误为止。 

另外,异常代码我们也整理一下: 
代码  类名            类型               描述 
============================================================================== 
11   FormElement     IndexOutOfBound   表单元素子集索引越界 
12   FormElement     IllegalArgument   非法的表单元素子集索引 
21   UploadRequest   IndexOutOfBound   文本元素索引越界 
22   UploadRequest   IllegalArgument   非法的文本元素索引 
23   UploadRequest   IndexOutOfBound   文件元素索引越界 
24   UploadRequest   NullRef           文件元素索引不存在 
25   UploadRequest   IllegalArgument   非法的表单元素索引 
26   UploadRequest   TooLargeFile      文件%fldname尺寸过大 
27   UploadRequest   TooLargeFiles     文件总尺寸过大 
28   UploadRequest   InvalidFileType   文件%fldname类型错误 

好了,下面的,就是我们的整个实现了: 
1。com.2yup.util.uploadrequest.class 
<% 
’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ 
’ 没有版权,欢迎拷贝或是作为商业用途。 
’ 如果要转载,能注明出处最好,我们会很感激您的支持;如果不方便,就算了,呵呵。 
’ 感谢各位常来2yup的网友(很多名字,写不下了,呵呵)长期热情的支持, 
’ 你们是我持久的动力。 
’  
’ 关于这个组件的详细信息,以及编程的全过程,可以来 
’ http://www.2yup.com/asp 
’ 的文档中心看个究竟。有任何疑问,欢迎来我们的论坛讨论,或是给我发email: 
’ miles2yup@hotmail.com 
’                                     ---- Miles [Yup Studio] ^ ^ 
’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ 

’========================================================================= 
’’ 这个,是存储文本域信息的的类。每一个name的文本域,对应一个这样的类。 
’========================================================================= 
Class FormElement 

 ’ m_开头,表示类成员变量。 
 Private m_dicItems 

 Private Sub Class_Initialize() 
   Set m_dicItems = Server.CreateObject("Scripting.Dictionary") 
 End Sub 

 ’ set nothing时激发。清理资源 
 Private Sub Class_Terminate() 
   Set m_dicItems = Nothing 
 End Sub 

 ’ count是咱们这个类的一个只读属性 
 Public Property Get Count() 
   Count = m_dicItems.Count 
 End Property 

 ’ value是一个默认属性。目的是得到值 
 Public Default Property Get value() 
   value = Item("") 
 End Property 

 ’ Name是得到文本域名称。就是<input name=xxx>里的xxx 
 Public Property Get Name() 
   Dim Keys 
   Keys = m_dicItems.Keys 
   Name = Keys(0) 
   Name = left(Name,instrrev(Name,"_")-1) 
 End Property 

 ’ Item属性用来得到重名表单域(比如checkbox)的某一个值 
 Public Property Get Item(index) 
   Dim Items, i 
   If isNumeric(index) Then ’是数字,合法! 
     If index > m_dicItems.Count-1 Then 
       err.raise 11,"IndexOutOfBound", "表单元素子集索引越界" 
     End If 
     Items = m_dicItems.Items 
     Item = Items(index) 
   ElseIf index = "" Then ’没给值?那就返回所有的!逗号分隔 
     Items = m_dicItems.Items 
     For i = 0 to m_dicItems.Count-1 
       If i = 0 Then 
         Item = Items(0) 
       Else 
         Item = Item & "," & Items(i) 
       End If 
     Next 
   Else ’给个一个不是数字的东东?出错! 
     err.raise 12,"IllegalArgument", "非法的表单元素子集索引" 
   End If 
 End Property 

 Public Sub Add(key, item) 
   m_dicItems.Add key, item 
 End Sub 

End Class 

’========================================================================= 
’’ 这个,是存储文件域信息的的类。每一个name的文件,对应一个这样的类。 
’========================================================================= 
Class FileElement 

 ’ m_开头,表示类成员变量。 
 Private m_strName 
 Private m_bData 
 Private m_bRawData 
 Private m_strContentType 
 Private m_strFilePath 
 Private m_strFileName 
 Private m_lSize 

 ’ Data是一个默认属性。目的是得到值 
 Public Default Property Get Data() 
   Data = m_bData 
 End Property 

 ’ 这个属性很尴尬——stream对象write方法要求的数据类型是 
 ’ "A Variant that contains an array of bytes to be written." 
 ’ 但是我却无法从一个二进制串中得到这个数据类型!的确很奇怪。所以,我打算 
 ’ 使用符合要求的原始数据m_bRawData。但是,vbs的类功能少得可怜,既不能传递 
 ’ 当前对象的引用来回访UploadRequest的m_bRawData也不能用inner class的方 
 ’ 法进行组织。为了保持方法的简洁,所以加了这个只写的RawData属性。 
 ’ 这个地方很值得改进。 
 Public Property Let RawData(data) 
   m_bRawData = data 
 End Property 

 ’ Name是得到文件域名称,就是<input type=file name=xxx>里的xxx 
 Public Property Get Name() 
   Name = m_strName 
 End Property 

 ’ ContentType是得到文件contentType 
 Public Property Get ContentType() 
   ContentType = m_strContentType 
 End Property 

  ’ FilePath是得到文件在客户端的路径 
 Public Property Get FilePath() 
   FilePath = m_strFilePath 
 End Property 

  ’ FilePath是得到文件在客户端的路径 
 Public Property Get FileName() 
   FileName = m_strFileName 
 End Property 

  ’ Size是得到文件大小 
 Public Property Get Size() 
   Size = m_lSize 
 End Property 

 Public Sub Add(name, data, contenttype, path) 
   m_strName = name 
   m_bData = data 
   m_strContentType = contenttype 
   m_strFilePath = path 
   m_strFileName = right(path, len(path)-instrrev(path, "\")) 
   m_lSize = lenb(data) 
 End Sub 

 Public Sub SaveTo(path) 
   Call SaveAs(path, m_strFileName) 
 End Sub 

 Public Sub SaveAs(path, name) 
   Call Save(path, name, True) 
 End Sub 

 Public Sub SaveWithoutOverwrite(path, name) 
   Call Save(path, name, False) 
 End Sub 

 Private Sub Save(path, name, isOverwrite) 
   Dim st, st2 
   ’这样就可以兼顾c:\xxx\和c:\xxx两种格式了 
   If right(path,1) <> "\" Then path = path & "\" 
   ’用两个stream对象,来截取我们要的内容 
   Set st = Server.CreateObject("ADODB.Stream") 
   Set st2 = Server.CreateObject("ADODB.Stream") 
   st.Type = 1 
   st.open 
   st2.Type = 1 
   st2.open 
   st.write m_bRawData 
   st.Position = instrb(m_bRawData,m_bData)-1 
   st.copyto st2, m_lSize 

   If isOverwrite Then ’覆盖保存 
     st2.SaveToFile path & name,2 
   Else ’不覆盖 
     st2.SaveToFile path & name 
   End If 

   st.Close 
   Set st = Nothing 
   st2.Close 
   Set st2 = Nothing 
 End Sub 

End Class 

’========================================================================= 
’’ 这个,是我们模拟的request类。我们用它完成asp的request完成不了的任务 :) 
’========================================================================= 
Class UploadRequest 

 Private m_dicForms 
 Private m_dicFiles 
 Private m_bRawData 
 Private m_lTotalBytes 
 Private m_strAllowedFilesList 
 Private m_strDeniedFilesList 
 Private m_lMaxFileSize 
 Private m_lTotalMaxFileSize 

 ’初始化类成员 
 Private Sub Class_Initialize() 
   m_lTotalBytes = 0 
   m_strAllowedFilesList = "" 
   m_strDeniedFilesList = "" 
   m_lMaxFileSize = -1 
   m_lTotalMaxFileSize = -1 
 End Sub 

 ’ set nothing时激发。清理资源 
 Private Sub Class_Terminate() 
   ’ 这些对象应该有自己的清理方法,咱就不管了 
   Set m_dicForms = Nothing 
   Set m_dicFiles = Nothing 
 End Sub 

 Public Sub Upload 
   Set m_dicForms = Server.CreateObject("Scripting.Dictionary") 
   Set m_dicFiles = Server.CreateObject("Scripting.Dictionary")     
   Call fill() 
 End Sub 

 ’存文件到指定路径 
 Public Sub SaveTo(path) 
   Dim fElement 
   ’调用FileElement自己的方法 
   For Each fElement In m_dicFiles 
     Call m_dicFiles.Item(fElement).SaveTo(path) 
   Next 
 End Sub 

 ’ 有了这个,就可以检查原始数据了 
 Public Property Get RawData() 
   RawData = m_bRawData 
 End Property 

 ’ 这一段丑陋的代码是为了实现ourRequest.Forms.Count这个功能。这个地方值得改进。 
 Public Property Get Forms() 
   Set Forms = New Counter 
   Forms.setCount(m_dicForms.Count) 
 End Property 

 ’ 这一段丑陋的代码是为了实现ourRequest.Files.Count这个功能。这个地方值得改进。 
 Public Property Get Files() 
   Set Files = New Counter 
   Files.setCount(m_dicFiles.Count) 
 End Property 

 ’只读的TotalBytes属性 
 Public Property Get TotalBytes() 
   TotalBytes = m_lTotalBytes 
 End Property 

 ’只写的AllowedFilesList属性,填入允许类型的扩展名,用|分隔 
 Public Property Let AllowedFilesList(afl) 
   m_strAllowedFilesList = afl 
 End Property 

 ’只写的DeniedFilesList属性,填入允许类型的扩展名,用|分隔 
 Public Property Let DeniedFilesList(dfl) 
   m_strDeniedFilesList = dfl 
 End Property 

 ’只写的MaxFileSize属性,填入各个允许上传文件的大小 
 Public Property Let MaxFileSize(mfs) 
   m_lMaxFileSize = mfs 
 End Property 
文章页数:[1] 
帮助你我他: 1.我有问题请教 2.我要投稿>>>
更多相关资料搜索:
热点文章
最新文章
相关文章
版权申明:除部分特别声明不要转载,或者授权本站独家播发的文章外,大家可以自由转载本站的原创文章,但原作者和来自本站的链接必须保留(非本站原创的,按照原来自一节,自行链接)。文章版权归本站和作者共有。
转载要求:转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印,亦不能抹去本站水印。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论  打印  刷新  推荐给朋友  返回顶部  关闭

网上大名: