主题:  一分钟搞定文件(包括图片)上传!

xpilot

职务:普通成员
等级:1
金币:1.0
发贴:192
#12001/5/24 15:22:20
今天到国外UD网站,www.udzone.com去转了一圈!发现一个很好的文件上传插件,下来一用!
不到一分钟就做出一个简单的文件上传页面!相比较而言,以前做文件上传时,要安装各种组件,还要手工添加代码,且及容易出错!真是我等懒人的福音阿:)
废话少说下面介绍一下如何使用:

1。当然是下它回来拉,地址:http://www.udzone.com/Downloads/PureUpload.mxp
UDzone还有很多好东西,不过最好注册一下先!

2。下下来做什么,当然是安装插件了(不要告诉我你不会安装UD插件哦:)

3。插件装完以后,你会发现在你的UD菜单下会多出
Windows -> Server Behaviors -> Pure ASP File Upload
当然也可以在Server Behaviors窗口中点+号,也会发现 Pure ASP File Upload

4。先在UD里创建一个 RecordSet这是少不了的,然后再分别创建一个
Insert Form 和 Insert File Field 域!当然了,再建两个按钮 Insert Button,分别命名为“上传”,“取消”,OK,各元素基本都齐了!

5。用鼠标点中Insert File Field,然后到Server Behaviors里点中那个+号,再选Pure ASP File Upload
这时弹出个小筐,内容分别是:
(1) Upload Directory 选择你要上传文件存放的目录,默认为当前网页的目录;
(2) Allowed extensions 这里选择上传文件类型,任何/图片/自定义,三种类型;
(3) Form 网页中提交按钮所在的from名;
(4) Go To URL 完成上传后转到的页面地址。

6。全部设置完成,点击确定保存网页,到IE里输出网址,点击“浏览...”按钮随便选个文件,点确定。回来点击“上传”按钮。OK,整个世界清净拉~~~~,跑到你设置的存放上传文件的目录去看看,考!怎么那么快文件就传过去拉:)(本地测试嘛)

OK,文件上传就这么简单,如果你熟练的话,一分钟就搞定!

附:测试上传文件 testupload.asp 源码

testupload.asp
------------------------------------------------------------------------
<%@LANGUAGE="VBSCRIPT"%>

<%
'*** File Upload to: , Extensions: "", Form: form1, Redirect: ""
'*** Pure ASP File Upload -----------------------------------------------------
' Copyright 2000 (c) George Petrov
'
' Script partially based on code from Philippe Collignon
' (http://www.asptoday.com/articles/20000316.htm)
'
' New features from GP:
' * Fast file save with ADO 2.5 stream object
' * new wrapper functions, extra error checking
' * UltraDev Server Behavior extension
'
' Version: 1.5.0
'------------------------------------------------------------------------------
Sub BuildUploadRequest(RequestBin)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
if PosEnd = 0 then
Response.Write "Form was submitted with no ENCTYPE=""multipart/form-data""
"
Response.Write "Please correct the form attributes and try again."
Response.End
end if
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries
Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
'Members variable of objects are put in a dictionary object
Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary")
'Get an object name
Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
Pos = InstrB(Pos,RequestBin,getByteString("name="))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
PosBound = InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 AND (PosFile 'Get Filename, content-type and content of file
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
FileName = Mid(FileName,InStrRev(FileName,"\")+1)
'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
value = FileName
valueBeg = PosBeg-1
valueLen = PosEnd-Posbeg
Else
'Get content of object
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
valueBeg = 0
valueEnd = 0
End If
'Add content to dictionary object
UploadControl.Add "value" , value    
UploadControl.Add "valueBeg" , valueBeg
UploadControl.Add "valueLen" , valueLen    
'Add dictionary object to main dictionary
UploadRequest.Add name, UploadControl    
'Loop to next object
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
End Sub

'String to byte string conversion
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
     char = Mid(StringStr,i,1)
     getByteString = getByteString & chrB(AscB(char))
Next
End Function

'Byte string to string conversion
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
     getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

Function UploadFormRequest(name)
on error resume next
if UploadRequest.Item(name) then
UploadFormRequest = UploadRequest.Item(name).Item("value")
end if
End Function

'Process the upload
UploadQueryString = Replace(Request.QueryString,"GP_upload=true","")
if mid(UploadQueryString,1,1) = "&" then
    UploadQueryString = Mid(UploadQueryString,2)
end if

GP_uploadAction = CStr(Request.ServerVariables("URL")) & "?GP_upload=true"
If (Request.QueryString <> "") Then
if UploadQueryString <> "" then
     GP_uploadAction = GP_uploadAction & "&" & UploadQueryString
end if
End If

If (CStr(Request.QueryString("GP_upload")) <> "") Then
GP_redirectPage = ""
If (GP_redirectPage = "") Then
GP_redirectPage = CStr(Request.ServerVariables("URL"))
end if

RequestBin = Request.BinaryRead(Request.TotalBytes)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin

GP_keys = UploadRequest.Keys
for GP_i = 0 to UploadRequest.Count - 1
GP_curKey = GP_keys(GP_i)
'Save all uploaded files
if UploadRequest.Item(GP_curKey).Item("FileName") <> "" then
GP_value = UploadRequest.Item(GP_curKey).Item("value")
GP_valueBeg = UploadRequest.Item(GP_curKey).Item("valueBeg")
GP_valueLen = UploadRequest.Item(GP_curKey).Item("valueLen")

if GP_valueLen = 0 then
Response.Write "An error has occured saving uploaded file!

"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "
"
Response.Write "File does not exists or is empty.
"
Response.Write "Please correct and try again"
          response.End
     end if

'Create a Stream instance
Dim GP_strm1, GP_strm2
Set GP_strm1 = Server.CreateObject("ADODB.Stream")
Set GP_strm2 = Server.CreateObject("ADODB.Stream")

'Open the stream
GP_strm1.Open
GP_strm1.Type = 1 'Binary
GP_strm2.Open
GP_strm2.Type = 1 'Binary

GP_strm1.Write RequestBin
GP_strm1.Position = GP_valueBeg
GP_strm1.CopyTo GP_strm2,GP_valueLen

'Create and Write to a File
GP_curPath = Request.ServerVariables("PATH_INFO")
GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/")) & "")
if Mid(GP_curPath,Len(GP_curPath),1) <> "/" then
GP_curPath = GP_curPath & "/"
end if
on error resume next
GP_strm2.SaveToFile Trim(Server.mappath(GP_curPath))& "\" & UploadRequest.Item(GP_curKey).Item("FileName"),2
if err then
Response.Write "An error has occured saving uploaded file!

"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "
"
Response.Write "Maybe the destination directory does not exist, or you don't have write permission.
"
Response.Write "Please correct and try again"
         err.clear
          response.End
     end if
end if
next

'*** GP NO REDIRECT
end if
if UploadQueryString <> "" then
UploadQueryString = UploadQueryString & "&GP_upload=true"
else
UploadQueryString = "GP_upload=true"
end if

%>
<%
set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_webdata_STRING
Recordset1.Source = "SELECT art_id FROM dbo.article"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
%>


testupload.asp





文件上传:









<%
Recordset1.Close()
%>



sunyabc

职务:普通成员
等级:1
金币:0.0
发贴:53
#22001/5/24 17:23:44
这个插件早就有的,我最早是在“高处不盛寒”的网页上看见,有详细的使用说明,我在自己做的新闻发布系统中也使用了他上传图片。

这个东西有以下几个毛病:
1。不能同时上传中文的文字。
2。不能限制文件的大小。
希望有高手来改改。让他真正的适合中国人。



jay_5d

职务:普通成员
等级:2
金币:0.0
发贴:223
#32001/5/25 16:04:45
是无组件的吗?


[img]http://go4.163.com/~liyuanhao/jay.GIF[/img] 现在倒数......2X30X24X60X60秒我就不再是学生了。 :(

qking

职务:普通成员
等级:1
金币:0.0
发贴:19
#42001/5/25 19:06:35
请问楼上的SUNYABC兄 “高处不盛寒”网站的网址是多少?



sunyabc

职务:普通成员
等级:1
金币:0.0
发贴:53
#52001/5/26 9:02:27
不用组件,下载地址是http://ultradev21.126.com



xpilot

职务:普通成员
等级:1
金币:1.0
发贴:192
#62001/5/27 2:41:33
jay老弟,当然是无组件的!我第一段说的很清楚了,有组件上传到处都有介绍,现在是无组件的希奇:)



jay_5d

职务:普通成员
等级:2
金币:0.0
发贴:223
#72001/5/27 12:40:40
嘿嘿,谢了。


[img]http://go4.163.com/~liyuanhao/jay.GIF[/img] 现在倒数......2X30X24X60X60秒我就不再是学生了。 :(

绿茶

职务:普通成员
等级:8
金币:10.0
发贴:19267
#82001/7/4 17:15:47
但用插件做与组件做有什么区别呀,还不是用别人做出来的东西呀,一点成就感都没有!!!



xpilot

职务:普通成员
等级:1
金币:1.0
发贴:192
#92001/7/4 17:37:27
哈!:)是啊用了别人的东西自然是没有什么成就感的!所以说还是提倡自己动手丰衣足食啊!不过呢,搞技术的也分三六九等。有人搞原创,也有人喜欢搞应用把更多时间用来做创意!也是罗卜白菜各有所好嘛! 用插件我觉得还是有一定积极意义的,至少原代码你是有,还可以学习别人的编成思路。 至于说用组件,我想能够编组件的人应该不会再用什么UD了吧:)



microlight

职务:普通成员
等级:1
金币:0.0
发贴:6
#102001/7/5 9:49:30
图片是能上传到目录,但图片地址信息不能加到数据库呀,好像说需要ADO 2.5,哪里有下载,不过虚拟主机服务商也比一定能允许在服务器上添加这个组件呀



microlight

职务:普通成员
等级:1
金币:0.0
发贴:6
#112001/7/6 14:45:56
图片上传终于搞定了:先使用pure asp file upload指定上传目录,然后针对数据库id字段创建一个记录集,再使用insert record把filefield name绑定到数据库的图片名称字段,保存。试一下,图片上传到了指定目录,图片文件名也加入了数据库,ok。



杏儿

职务:普通成员
等级:1
金币:0.0
发贴:56
#122001/7/6 16:33:50
好感谢你吔。



水滴

职务:普通成员
等级:1
金币:0.0
发贴:8
#132001/7/8 14:30:59
sunyabc在上个贴子中说
引用:
这个插件早就有的,我最早是在“高处不盛寒”的网页上看见,有详细的使用说明,我在自己做的新闻发布系统中也使用了他上传图片。

这个东西有以下几个毛病:
1。不能同时上传中文的文字。
2。不能限制文件的大小。
希望有高手来改改。让他真正的适合中国人。



个人觉得是最棒的UD中一个插件,以前很早用过,啥问题都能解决,就是不能解决图文混传(文字用中文)今天搞了个新版的再去试试看,解决中文的问题



xpilot

职务:普通成员
等级:1
金币:1.0
发贴:192
#142001/7/9 13:28:00
哦,出新版了?什么地方有下?UDzone.com还是提供原来的!麻烦发一个来给我,xpilot@21cn.com谢了!



Nevertheless

职务:普通成员
等级:1
金币:0.0
发贴:5
#152001/7/22 10:48:51
microlight老兄,能不能详细说明一下如何把图片地址信息加入到数据库里面,万分感谢!!!