下面将介绍一系列可以不用组件,而使用纯粹的ASP代码来上传文件
呵呵,我想这将给很多拥有个人主页的网友带来极大的方便。
这个纯ASP代码由三个包含文件组成,代码中只使用了FileSystemObject
和Direction两个ASP固有对象。而不需要任何附加的组件,注意,为了保证
这段代码的出处,我没有对代码中的任何地方进行过修改。
希望能够对大家有所帮助:
文件fupload.inc
文件futils.inc
文件fformat.inc
下面的第一个例子为只是将客户端的文件上传到服务端的例子
第二个例子为将文件内容保存入数据库中。
文件fupload.asp
<%
dim ResultHTML
'Some value greater than default of 60s (According to upload size.)
'The maximum speed is about 100kB/s for IIS4, P200 and local upload, 4kB/s for modem users.
Server.ScriptTimeout = 400
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then 'Request method must be "POST" for get the fields
' BeginTimer 'Starts timer.
'************************************************* Main Upload - start
Dim Fields
' on error resume next
'Set upload limit to 10M
UploadSizeLimit = 10000000
'Gets uploaded fields
Set Fields = GetUpload()
'There are all of form fields in the Fields object. Example :
'Fields("File1").ContentType - content type of File1 field
'Fields("File1").value - Binary value of File1 field
ResultHTML = ""
If Err = 0 Then 'Upload was OK
'Write statistics about upload
dim Field
For Each Field In Fields.Items
ResultHTML = ResultHTML & "
Field :
" & LogF(Field.name) & ", Length :
" & LogFn(Field.Length) & ", Content-Type :
" & LogF(Field.ContentType) & ", SourceFileName :?b>" & LogF(Field.FileName) & ""
Next
'Saves the fields to the disk, writes result to the client and writes log.
'See utils.inc. You can change the function to save the files to another location.
ResultHTML = ResultHTML & "
" & SaveUpload(Fields, Server.MapPath("."), LogFolder)
Else 'Error in upload. Write the error
ResultHTML = ResultHTML & "
Error : " & Err.Description
End If
On Error GoTo 0
Fields = Empty 'Clear the variable
'************************************************* Main Upload - end
' EndTimer 'Writes info about consumed time.
End If 'Request method must be "POST"
%>
<%'upload.inc, contains GetUpload function, Required for upload - only the one file%>
<%'utils.inc, contains SaveUpload function%>
<%'format.inc, contains head and Foot function, optional.%>
<%=Head("Sample multiple binary files upload via ASP", "Demonstrates using of the ByteArray class for working with binary data from Request.BinaryRead.")%>
?%=ResultHTML%>
<%=Foot%>
文件fdbutl.asp将文件内容保存如数据库中
<%'upload.inc, contains GetUpload function, Required for upload - only the one file%>
<%'format.inc, contains head and Foot function, optional.%>
<%=Head("Sample database upload via ASP", "Demonstrates using of the ByteArray class for working with binary data from Request.BinaryRead.")%>
<%=Foot%>
下面是w3 upload组件的例子
jscript: var w3u = Server.CreateObject( "w3.upload" );
var fileField = w3u.form( "myFile" );
var fileName = fileField.fileName;
fileField.SaveToFile( "c:\\temp\\" +fileName );
vbscript: dim w3u, fileField,
fileName SET w3u = Server.CreateObject( "w3.upload" )
SET fileField = w3u.form( "myFile" )
fileName = fileField.fileName
fileField.SaveToFile "c:\temp\" & fileName
Setting maximum filesize
dim w3u, fileField, fileName
SET w3u = Server.CreateObject( "w3.upload" )
SET fileField = w3u.form( "myFile" ) ' As the maxsize is set
in bytes, we calculate 1024 bytes times 1024 ' and then times 8, which gives us a 8mb maxsize FileField.maxSize = 1024*1024*8
fileName = fileField.fileName
ON error resume NEXT
fileField.SaveToFile "c:\temp\" & fileName ' If the maxsize is exceeded, w3 Upload throws an error
IF err.number <> 0 THEN
Response.write "File too big"
END IF
Limit uploads to a certain content type dim w3u, fileField, fileName
SET w3u = Server.CreateObject( "w3.upload" )
SET fileField = w3u.form("myFile" )
IF FileField.contentType = "image/gif" THEN
fileField.SaveToFile "c:\tem p\" & fileName
ELSE
Response.write "Not a valid content type"
END IF
w3.FormField
error '8000ffff' Win32 Error. Code: 5. Access is denied The error (5) indicates a permission problem on the target path.
Check that there's sufficient WRITE permissions in this directory (or file you're wrtiting to).
Note that if you are using Request.ServerVariables ( "APPL_PHYSICAL_PATH" ), then this is the APPLICATION path, not necessarily the same path as the current ASP file is executed in.