下面的这段代码上传的文件名是文件原名,我想改成上传系统时间作为文件名,使文件不会重复,请问该怎么改?
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="Connections/u_upload.asp" -->
Sub BuildUploadRequest(RequestBin,UploadDirectory,storeType,sizeLimit,nameConflict)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
if PosEnd = 0 then
Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
Response.Write "Please correct the form attributes and try again."
Response.End
end if
'Check ADO Version
set checkADOConn = Server.CreateObject("ADODB.Connection"
adoVersion = CSng(checkADOConn.Version)
set checkADOConn = Nothing
if adoVersion < 2.5 then
Response.Write "<b>You don't have ADO 2.5 installed on the server.</b><br>"
Response.Write "The File Upload extension needs ADO 2.5 or greater to run properly.<br>"
Response.Write "You can download the latest MDAC (ADO is included) from <a href=""www.microsoft.com/data"">www.microsoft.com/data</a><br>"
Response.End
end if
'Check content length if needed
Length = CLng(Request.ServerVariables("HTTP_Content_Length"
) 'Get Content-Length header
If "" & sizeLimit <> "" Then
sizeLimit = CLng(sizeLimit)
If Length > sizeLimit Then
Request.BinaryRead (Length)
Response.Write "Upload size " & FormatNumber(Length, 0) & "B exceeds limit of " & FormatNumber(sizeLimit, 0) & "B"
Response.End
End If
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<PosBound) Then
'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
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 "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName"
& "<br>"
Response.Write "File does not exists or is empty.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
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
dim mb_folder
mb_folder = UploadFormRequest("u_type"
GP_curPath = Request.ServerVariables("PATH_INFO"
GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/"
) & mb_folder)
if Mid(GP_curPath,Len(GP_curPath),1) <> "/" then
GP_curPath = GP_curPath & "/"
end if
GP_CurFileName = UploadRequest.Item(GP_curKey).Item("FileName"
GP_FullFileName = Trim(Server.mappath(GP_curPath))& "\" & GP_CurFileName
'Check if the file alreadu exist
GP_FileExist = false
Set fso = CreateObject("Scripting.FileSystemObject"
If (fso.FileExists(GP_FullFileName)) Then
GP_FileExist = true
End If
if nameConflict = "error" and GP_FileExist then
Response.Write "<B>File already exists!</B><br><br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
GP_strm1.Close
GP_strm2.Close
response.End
end if
if ((nameConflict = "over" or nameConflict = "uniq"
and GP_FileExist) or (NOT GP_FileExist) then
if nameConflict = "uniq" and GP_FileExist then
Begin_Name_Num = 0
while GP_FileExist
Begin_Name_Num = Begin_Name_Num + 1
GP_FullFileName = Trim(Server.mappath(GP_curPath))& "\" & fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
GP_FileExist = fso.FileExists(GP_FullFileName)
wend
UploadRequest.Item(GP_curKey).Item("FileName"
= fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
UploadRequest.Item(GP_curKey).Item("value"
= UploadRequest.Item(GP_curKey).Item("FileName"
end if
on error resume next
GP_strm2.SaveToFile GP_FullFileName,2
if err then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName"
& "<br>"
Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
err.clear
GP_strm1.Close
GP_strm2.Close
response.End
end if
GP_strm1.Close
GP_strm2.Close
if storeType = "path" then
UploadRequest.Item(GP_curKey).Item("value"
= GP_curPath & UploadRequest.Item(GP_curKey).Item("value"
end if
on error goto 0
end if
end if
next
End Sub
'把普通字符串转成二进制字符串函数
Function getByteString(StringStr)
getByteString=""
For i = 1 To Len(StringStr)
XP_varchar = mid(StringStr,i,1)
XP_varasc = Asc(XP_varchar)
If XP_varasc < 0 Then
XP_varasc = XP_varasc + 65535
End If
If XP_varasc > 255 Then
XP_varlow = Left(Hex(Asc(XP_varchar)),2)
XP_varhigh = right(Hex(Asc(XP_varchar)),2)
getByteString = getByteString & chrB("&H" & XP_varlow) & chrB("&H" & XP_varhigh)
Else
getByteString = getByteString & chrB(AscB(XP_varchar))
End If
Next
End Function
'把二进制字符串转换成普通字符串函数
Function getString(StringBin)
getString =""
Dim XP_varlen,XP_vargetstr,XP_string,XP_skip
XP_skip = 0
XP_string = ""
If Not IsNull(StringBin) Then
XP_varlen = LenB(StringBin)
For i = 1 To XP_varlen
If XP_skip = 0 Then
XP_vargetstr = MidB(StringBin,i,1)
If AscB(XP_vargetstr) > 127 Then
XP_string = XP_string & Chr(AscW(MidB(StringBin,i+1,1) & XP_vargetstr))
XP_skip = 1
Else
XP_string = XP_string & Chr(AscB(XP_vargetstr))
End If
Else
XP_skip = 0
End If
Next
End If
getString = XP_string
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, "updown", "path", "5000000", "over"
'*** GP NO REDIRECT
end if
if UploadQueryString <> "" then
UploadQueryString = UploadQueryString & "&GP_upload=true"
else
UploadQueryString = "GP_upload=true"
end if
%>
<%
' *** Edit Operations: (Modified for File Upload) declare variables
MM_editAction = CStr(Request.ServerVariables("URL"
) 'MM_editAction = CStr(Request("URL"
)
If (UploadQueryString <> ""
Then
MM_editAction = MM_editAction & "?" & UploadQueryString
End If
' boolean to abort record edit
MM_abortEdit = false
' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: (Modified for File Upload) set variables
If (CStr(UploadFormRequest("MM_insert"
) <> ""
Then
MM_editConnection = MM_u_upload_STRING
MM_editTable = "U_upload"
MM_editRedirectUrl = "default.asp"
MM_fieldsStr = "U_txt|value|file|value"
MM_columnsStr = "U_txt|',none,''|U_filename|',none,''"
' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|"
MM_columns = Split(MM_columnsStr, "|"
' set the form values
For i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(i+1) = CStr(UploadFormRequest(MM_fields(i)))
Next
' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And UploadQueryString <> ""
Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And UploadQueryString <> ""
Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & UploadQueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & UploadQueryString
End If
End If
End If
%>
<%
' *** Insert Record: (Modified for File Upload) construct a sql insert statement and execute it
If (CStr(UploadFormRequest("MM_insert"
) <> ""
Then
' create the sql insert statement
MM_tablevalues = ""
MM_dbvalues = ""
For i = LBound(MM_fields) To UBound(MM_fields) Step 2
FormVal = MM_fields(i+1)
MM_typeArray = Split(MM_columns(i+1),","
Delim = MM_typeArray(0)
If (Delim = "none"
Then Delim = ""
AltVal = MM_typeArray(1)
If (AltVal = "none"
Then AltVal = ""
EmptyVal = MM_typeArray(2)
If (EmptyVal = "none"
Then EmptyVal = ""
If (FormVal = ""
Then
FormVal = EmptyVal
Else
If (AltVal <> ""
Then
FormVal = AltVal
ElseIf (Delim = "'"
Then ' escape quotes
FormVal = "'" & Replace(FormVal,"'","''"
& "'"
Else
FormVal = Delim + FormVal + Delim
End If
End If
If (i <> LBound(MM_fields)) Then
MM_tablevalues = MM_tablevalues & ","
MM_dbvalues = MM_dbvalues & ","
End if
MM_tablevalues = MM_tablevalues & MM_columns(i)
MM_dbvalues = MM_dbvalues & FormVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tablevalues & "
values (" & MM_dbvalues & "
"
If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
If (MM_editRedirectUrl <> ""
Then
Response.Redirect(MM_editRedirectUrl)
End If
End If
End If
%>
<html>
<head>
<title>上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
<!--
function getFileExtension(filePath) { //v1.0
fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\\')+1,filePath.length));
return fileName.substring(fileName.lastIndexOf('.')+1,fileName.length);
}
function checkFileUpload(form,extensions) { //v1.0
document.MM_returnvalue = true;
if (extensions && extensions != '') {
for (var i = 0; i<form.elements.length; i++) {
field = form.elements[i];
if (field.type.toUpperCase() != 'FILE') continue;
if (field.value == '') {
alert('文件框中必须保证已经有文件被选中!');
document.MM_returnvalue = false;field.focus();break;
}
if (extensions.toUpperCase().indexOf(getFileExtension(field.value).toUpperCase()) == -1) {
alert('这种文件类型不允许上传!.\n只有以下类型的文件才被允许上传: ' + extensions + '.\n请选择别的文件并重新上传.');
document.MM_returnvalue = false;field.focus();break;
} } }
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form ACTION="<%=MM_editAction%>" METHOD="POST" name="form1" enctype="multipart/form-data" onSubmit="checkFileUpload(this,'');return document.MM_returnvalue">
<table width="310" border="1" cellspacing="0" cellpadding="0">
<tr align="center">
<td colspan="2">文件上传</td>
</tr>
<tr>
<td width="65">文件类型</td>
<td width="231"><input name="u_type" type="hidden" id="u_type" value="updown"> </td>
</tr>
<tr>
<td width="65">文件说明</td>
<td width="231">
<input type="text" name="U_txt">
</td>
</tr>
<tr>
<td width="65">选择文件</td>
<td width="231">
<input type="file" name="file">
</td>
</tr>
<tr>
<td width="65"> </td>
<td width="231">
<input type="submit" name="Submit" value="提交" >
</td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="true">
</form>
</body>
</html>