By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Uploading Files

P: 1
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form.

I am using some code that I found from Jacob at asp101.com.
http://www.asp101.com/articles/jacob/scriptupload.asp

My three files are:
Syllabus.htm

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. </head><body>
  6. <table width="500" border="1" frame="box" rules="none" align="center" cellpadding="0" cellspacing="0">
  7. <FORM name="form" ENCTYPE="multipart/form-data" METHOD="POST" ACTION="uploadexmple.asp">
  8.  <tr><TD colspan="4"><strong>Upload syllabus (one file)</strong></td>
  9. </tr><tr><td colspan="4"><INPUT TYPE=FILE SIZE=50 NAME="FILE1"></td></tr><TR><td colspan="4">
  10.    <div align="right">
  11.      <input type="reset" name="reset" /><input type="submit" name="Action" value="Submit">
  12.    </div></TD>
  13. </TR></form></Table>
  14. </body>
  15. </html>
Upload.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. Class FileUploader
  4.     Public  Files
  5.     Private mcolFormElem
  6.  
  7.     Private Sub Class_Initialize()
  8.         Set Files = Server.CreateObject("Scripting.Dictionary")
  9.         Set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
  10.     End Sub
  11.  
  12.     Private Sub Class_Terminate()
  13.         If IsObject(Files) Then
  14.             Files.RemoveAll()
  15.             Set Files = Nothing
  16.         End If
  17.         If IsObject(mcolFormElem) Then
  18.             mcolFormElem.RemoveAll()
  19.             Set mcolFormElem = Nothing
  20.         End If
  21.     End Sub
  22.  
  23.     Public Property Get Form(sIndex)
  24.         Form = ""
  25.         If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
  26.     End Property
  27.  
  28.     Public Default Sub Upload()
  29.         Dim biData, sInputName
  30.         Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
  31.         Dim nPosFile, nPosBound
  32.  
  33.         biData = Request.BinaryRead(Request.TotalBytes)
  34.         nPosBegin = 1
  35.         nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
  36.  
  37.         If (nPosEnd-nPosBegin) <= 0 Then Exit Sub
  38.  
  39.         vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
  40.         nDataBoundPos = InstrB(1, biData, vDataBounds)
  41.  
  42.         Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))
  43.  
  44.             nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
  45.             nPos = InstrB(nPos, biData, CByteString("name="))
  46.             nPosBegin = nPos + 6
  47.             nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
  48.             sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
  49.             nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
  50.             nPosBound = InstrB(nPosEnd, biData, vDataBounds)
  51.  
  52.             If nPosFile <> 0 And  nPosFile < nPosBound Then
  53.                 Dim oUploadFile, sFileName
  54.                 Set oUploadFile = New UploadedFile
  55.  
  56.                 nPosBegin = nPosFile + 10
  57.                 nPosEnd =  InstrB(nPosBegin, biData, CByteString(Chr(34)))
  58.                 sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
  59.                 oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\"))
  60.  
  61.                 nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
  62.                 nPosBegin = nPos + 14
  63.                 nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
  64.  
  65.                 oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
  66.  
  67.                 nPosBegin = nPosEnd+4
  68.                 nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
  69.                 oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
  70.  
  71.                 If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
  72.             Else
  73.                 nPos = InstrB(nPos, biData, CByteString(Chr(13)))
  74.                 nPosBegin = nPos + 4
  75.                 nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
  76.                 If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
  77.             End If
  78.  
  79.             nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
  80.         Loop
  81.     End Sub
  82.  
  83.     'String to byte string conversion
  84.     Private Function CByteString(sString)
  85.         Dim nIndex
  86.         For nIndex = 1 to Len(sString)
  87.            CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
  88.         Next
  89.     End Function
  90.  
  91.     'Byte string to string conversion
  92.     Private Function CWideString(bsString)
  93.         Dim nIndex
  94.         CWideString =""
  95.         For nIndex = 1 to LenB(bsString)
  96.            CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1))) 
  97.         Next
  98.     End Function
  99. End Class
  100.  
  101. Class UploadedFile
  102.     Public ContentType
  103.     Public FileName
  104.     Public FileData
  105.  
  106.     Public Property Get FileSize()
  107.         FileSize = LenB(FileData)
  108.     End Property
  109.  
  110.     Public Sub SaveToDisk(sPath)
  111.         Dim oFS, oFile
  112.         Dim nIndex
  113.  
  114.         If sPath = "" Or FileName = "" Then Exit Sub
  115.         If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"
  116.  
  117.         Set oFS = Server.CreateObject("Scripting.FileSystemObject")
  118.         If Not oFS.FolderExists(sPath) Then Exit Sub
  119.  
  120.         Set oFile = oFS.CreateTextFile(sPath & FileName, True)
  121.  
  122.         For nIndex = 1 to LenB(FileData)
  123.             oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
  124.         Next
  125.  
  126.         oFile.Close
  127.     End Sub
  128.  
  129.     Public Sub SaveToDatabase(ByRef oField)
  130.         If LenB(FileData) = 0 Then Exit Sub
  131.  
  132.         If IsObject(oField) Then
  133.             oField.AppendChunk FileData
  134.         End If
  135.     End Sub
  136.  
  137. End Class
  138. %>
& uploadexmple.asp

Expand|Select|Wrap|Line Numbers
  1. <%@ Language=VBScript %>
  2. <%Option Explicit%>
  3. <!-- #include file="upload.asp" -->
  4. <%
  5.  
  6. 'NOTE - YOU MUST HAVE VBSCRIPT v5.0 INSTALLED ON YOUR WEB SERVER
  7. '       FOR THIS LIBRARY TO FUNCTION CORRECTLY. YOU CAN OBTAIN IT
  8. '       FREE FROM MICROSOFT WHEN YOU INSTALL INTERNET EXPLORER 5.0
  9. '       OR LATER.
  10.  
  11.  
  12. ' Create the FileUploader
  13. Dim Uploader, File, Pathhtm
  14. Set Uploader = New FileUploader
  15.  
  16. ' This starts the upload process
  17. Uploader.Upload()
  18.  
  19. '******************************************
  20. ' Use [FileUploader object].Form to access 
  21. ' additional form variables submitted with
  22. ' the file upload(s). (used below)
  23. '******************************************
  24.  
  25.  
  26. ' Check if any files were uploaded
  27. If Uploader.Files.Count = 0 Then
  28.     Response.Write "File(s) not uploaded."
  29. Else
  30.     ' Loop through the uploaded files
  31.     For Each File In Uploader.Files.Items
  32.  
  33.     File.SaveToDisk "path to save to"        
  34.  
  35.  
  36.         ' Output the file details to the browser
  37.  
  38.         Response.Write "<strong>Success!</strong><br>"
  39.         Response.Write "File Uploaded: " & File.FileName & "<br>"
  40.  
  41.     Next
  42. End If
  43.  
  44.  %>
The problem I am having is that it only randomly works. For example, today I was successful at uploading a file that is 30KB, but failed with one that is 6MB and another that is 15KB.

Is there something I am missing? Any help would be really appreciated.
Jul 30 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.