Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File upload via ASP using forms

Status
Not open for further replies.

lengoo

IS-IT--Management
Jan 15, 2002
381
GH
Hi guys
Is there a quick and easy way to do a file upload using asp? I have managed to get the form object with the file working so you can browse and it returns the full name and path of the selected file. From here, would it be easy to grab that file and save to a location with a bit of simple coding?
Many thanks
 
Hi,
I found something very similar, i'm sure on this site but cant find it anymore...

Anyway I've pasted the code below, you may be able to cut/paste.

This is a pure ASP solution which doesnt need any activex controls etc and seems to work quite well.

I use the 1st piece of code in an Include file (Upload.inc)
with the 2nd embedded in a form allowing the user to browse to the appropriate files

Good Luck

PaulSc
--------------------------------------------------------
<%
Class FileUploader
Public Files
Private mcolFormElem

Private Sub Class_Initialize()
Set Files = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
Set mcolFormElem = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
End Sub

Private Sub Class_Terminate()
If IsObject(Files) Then
Files.RemoveAll()
Set Files = Nothing
End If
If IsObject(mcolFormElem) Then
mcolFormElem.RemoveAll()
Set mcolFormElem = Nothing
End If
End Sub

Public Property Get Form(sIndex)
Form = &quot;&quot;
If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
End Property

Public Default Sub Upload()
Dim biData, sInputName
Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
Dim nPosFile, nPosBound

biData = Request.BinaryRead(Request.TotalBytes)
nPosBegin = 1
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))

If (nPosEnd-nPosBegin) <= 0 Then Exit Sub

vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
nDataBoundPos = InstrB(1, biData, vDataBounds)

Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString(&quot;--&quot;))

nPos = InstrB(nDataBoundPos, biData, CByteString(&quot;Content-Disposition&quot;))
nPos = InstrB(nPos, biData, CByteString(&quot;name=&quot;))
nPosBegin = nPos + 6
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
nPosFile = InstrB(nDataBoundPos, biData, CByteString(&quot;filename=&quot;))
nPosBound = InstrB(nPosEnd, biData, vDataBounds)

If nPosFile <> 0 And nPosFile < nPosBound Then
Dim oUploadFile, sFileName
Set oUploadFile = New UploadedFile

nPosBegin = nPosFile + 10
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, &quot;\&quot;))

nPos = InstrB(nPosEnd, biData, CByteString(&quot;Content-Type:&quot;))
nPosBegin = nPos + 14
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))

oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))

nPosBegin = nPosEnd+4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)

If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
Else
nPos = InstrB(nPos, biData, CByteString(Chr(13)))
nPosBegin = nPos + 4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
End If

nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
Loop
End Sub

'String to byte string conversion
Private Function CByteString(sString)
Dim nIndex
For nIndex = 1 to Len(sString)
CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
Next
End Function

'Byte string to string conversion
Private Function CWideString(bsString)
Dim nIndex
CWideString =&quot;&quot;
For nIndex = 1 to LenB(bsString)
CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1)))
Next
End Function
End Class

Class UploadedFile
Public ContentType
Public FileName
Public FileData

Public Property Get FileSize()
FileSize = LenB(FileData)
End Property

Public Sub SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex

If sPath = &quot;&quot; Or FileName = &quot;&quot; Then Exit Sub
If Mid(sPath, Len(sPath)) <> &quot;\&quot; Then sPath = sPath & &quot;\&quot;

Set oFS = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
If Not oFS.FolderExists(sPath) Then Exit Sub

Set oFile = oFS.CreateTextFile(sPath & FileName, True)

For nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next

oFile.Close
End Sub

Public Sub SaveToDatabase(ByRef oField)
If LenB(FileData) = 0 Then Exit Sub

If IsObject(oField) Then
oField.AppendChunk FileData
End If
End Sub

End Class
%>

------------------------------------------------------
Dim Uploader, File,TimeOut,UploadLoc

Server.ScriptTimeout = 240

Set Uploader = New FileUploader

Uploader.Upload()

If Uploader.Files.Count = 0 Then
Response.Write &quot;<h2>File(s) were not uploaded succesfully. Please contact the HelpDesk</h2><p></p>&quot;
Response.Flush
Else
For Each File In Uploader.Files.Items
UploadLoc = &quot;G:\WebSystems\Tracking\Uploadedfiles\&quot; ' Physical temp Location
File.SaveToDisk UploadLoc

Response.Write &quot;File Uploaded: &quot; & File.FileName & &quot; - Size: &quot; & File.FileSize & &quot; bytes - To &quot; & UploadLoc & &quot;<br>&quot;
Response.Flush
Next
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top