u p a t t a c h . a s p :
<%
'********************************************************************************
'Property Variables
'********************************************************************************
const ForReading = 1
const ForWriting = 2
const ForAppending = 3
dim FileCount 'Number of files uploaded
dim FieldCount 'Number of fields uploaded
dim Path 'Path to store files in
dim Dict 'pointer to a Scripting.Dictionary object with form data
Path = Server.mappath(".") & "\"
FileCount = 0
FieldCount = 0
Dict = Null
Function Init()
Dim tBytes
Dim binData
Dim scrDict
tBytes = Request.TotalBytes
RequestBin = Request.BinaryRead(tBytes)
Set scrDict = Server.CreateObject("Scripting.Dictionary")
PosBeg = 1
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(13)))
If PosEnd < 2 Then
Set Dict = Server.CreateObject("Scripting.Dictionary")
Exit Function
End If
boundary = MidB(RequestBin, PosBeg, PosEnd - PosBeg)
BoundaryPos = InStrB(1, RequestBin, boundary)
Do Until (BoundaryPos = InStrB(RequestBin, boundary & getByteString("--")))
Dim UploadControl
Set UploadControl = Server.CreateObject("Scripting.Dictionary")
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)
If PosFile <> 0 And (PosFile < PosBound) Then
FileCount = FileCount + 1
PosBeg = PosFile + 10
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(34)))
FileName = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
UploadControl.Add "FileName", FileName
Pos = InStrB(PosEnd, RequestBin, getByteString("Content-Type:"))
PosBeg = Pos + 14
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(13)))
ContentType = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
UploadControl.Add "ContentType", ContentType
PosBeg = PosEnd + 4
PosEnd = InStrB(PosBeg, RequestBin, boundary) - 2
Value = MidB(RequestBin, PosBeg, PosEnd - PosBeg)
Else
FieldCount = FieldCount + 1
Pos = InStrB(Pos, RequestBin, getByteString(Chr(13)))
PosBeg = Pos + 4
PosEnd = InStrB(PosBeg, RequestBin, boundary) - 2
Value = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
End If
UploadControl.Add "Value", Value
scrDict.Add Name, UploadControl
BoundaryPos = InStrB(BoundaryPos + LenB(boundary), RequestBin, boundary)
Loop
Set Dict = scrDict
Set scrDict = Nothing
End Function
Function saveAs( sHTMLFormField, sNewFile)
If Dict.Exists(sHTMLFormField) And Len(getFileName(sHTMLFormField)) > 0 Then
binData = Dict.Item( sHTMLFormField).Item("Value")
binData = getString( binData)
Dim sFilePath
sFilePath = Path & getFileName(sHTMLFormField)
If Len( sNewFile) <> 0 Then sFilePath = sNewFile
Set oFSO = Server.CreateObject( "Scripting.FileSystemObject") '- create the transfer file using Scripting.FileSystemObject ...
Set oTextStream = oFSO.CreateTextFile( sFilePath, True) '- create a file binary write
oTextStream.Write( binData) '- write binData to the file
oTextStream.Close '- close the file
saveAs = True
Else
' Response.Write( "File associated with HTML <FORM> field name <INPUT NAME=" & sHTMLFormField & "> not found!")
saveAs = False
End If
End Function
Function getData(sHTMLFormField)
If Dict.Exists(sHTMLFormField) Then
getData = Dict.Item( sHTMLFormField).Item("Value")
Else
getData = ""
End If
End Function
Function getFileName(sHTMLFormField)
Dim strHTMLFormField
If Dict.Exists(sHTMLFormField) Then
strHTMLFormField = Dict.Item( sHTMLFormField).Item("FileName")
Else
strHTMLFormField = ""
End If
Dim tPos
Dim strRtn
strRtn = ""
tPos = InStrRev(strHTMLFormField, "\")
If tPos = 0 Or IsNull(tPos) Then
strRtn = strHTMLFormField
Else
strRtn = Right(strHTMLFormField, Len(strHTMLFormField) - tPos)
End If
getFileName = strRtn
End Function
Function getContentType(sHTMLFormField)
If Dict.Exists(sHTMLFormField) Then
getContentType = Dict.Item( sHTMLFormField).Item("ContentType")
Else
getContentType = ""
End If
End Function
Function getString(StringBin)
Dim strRtn
strRtn = ""
For intCount = 1 To LenB(StringBin)
strRtn = strRtn & Chr(AscB(MidB(StringBin, intCount, 1)))
Next
getString = strRtn
End Function
Function getByteString(StringStr)
Dim strRtn
strRtn = ""
For i = 1 To Len(StringStr)
Char = Mid(StringStr, i, 1)
strRtn = strRtn & ChrB(AscB(Char))
Next
getByteString = strRtn
End Function
%>
<HTML>
<HEAD>
</HEAD>
<BODY onunload=window.close();>
<A HREF="uploadtest.asp">Clear/Refresh</A>
<%
Dim UpName
If Len( Request.TotalBytes) > 0 Then
Init '- function in uploadit.inc that pulls out the file and all the form data
Response.Expires=0
Response.ExpiresAbsolute=#May 31,1996 13:30:15#
Server.ScriptTimeout = 100
Response.Write("<BR>Uploaded File Name: " & getFileName("fName"))
' Response.Write("<BR>Save Path: " & Path)
Response.Write("<BR>" & saveAs("fName",""))
UpName = saveAs("fName","")
' Response.Write("<BR>Results of trying to save a file you didnt upload: " & saveAs("txtName",""))
Response.Write("<BR>")
End If
%>
<BR>
<!--<INPUT type="text" id=AttachName_R name="fName" value=<%=UpName%>> -->
<FORM name=frmTest onsubmit="createCookie('filename',AttachName_R.value,1);" ACTION=upAttach.asp method=post enctype="multipart/form-data">
<script language=javascript>
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
</script>
<INPUT type="file" id=AttachName_R name="fName"><BR>
<INPUT TYPE=SUBMIT NAME=cmdSubmit VALUE="SUBMIT File">
</FORM>
</BODY>
</HTML>