Results 1 to 3 of 3

Thread: Uploading a File to A Server from VB

  1. #1

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Uploading a File to A Server from VB

    Now, i'm afraid to make use of this code you must have your own website, since it requires server side-scripting.

    First off you need to copy this code into a file on your server, preferably in the root site folder.

    Code:
    <%
    dim filename,linewrite,fs,f,append
    	filename=request.QueryString("filename")
    	linewrite=request.QueryString("linewrite")
    	append=request.QueryString("append")
    	
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set f=fs.OpenTextFile(Server.MapPath(filename),append,true)
    	f.WriteLine(linewrite)
    	f.close
    set fs=nothing
    set f=nothing
    	
    	
    %>
    Then you download the project included, which contains one form, with one inet control, one label and a button.

    The code for the form is:

    VB Code:
    1. Dim filename As String
    2. Dim serverfile As String
    3. Dim aspuploaderurl As String
    4. Dim lincount As Long
    5. Dim urlstring As String
    6. Dim i As Long
    7. Dim append As String
    8. Private Sub upload_file_Click()
    9. filename = InputBox("Enter Filename")
    10. serverfile = InputBox("Enter Server Path")
    11. aspuploaderurl = InputBox("Enter Uploader File URL")
    12.  
    13. On Error GoTo skipfileread
    14. Open filename For Input As #1
    15. Do
    16.  
    17.     Line Input #1, countstring
    18.     lincount = lincount + 1
    19.  
    20. Loop
    21. Close #1
    22. skipfileread:
    23. Close
    24. On Error Resume Next
    25.  
    26. Open filename For Input As #1
    27.  
    28.  
    29. For i = 1 To lincount
    30. status_label = Str(FormatNumber((i / lincount) * 100, 3)) + "%"
    31. DoEvents
    32. If i = 1 Then
    33. append = "2"
    34. Else
    35. append = "8"
    36. End If
    37. On Error Resume Next
    38. Line Input #1, writeline
    39.  
    40. urlstring = aspuploaderurl
    41. urlstring = urlstring + "?" + "filename=" + serverfile + "&" + "append=" + append + "&" + "linewrite=" + writeline
    42. uploadinet.OpenURL (urlstring)
    43.  
    44.  
    45. Next i
    46.  
    47. Close #1
    48. skipall:
    49.    If Err <> 0 Then
    50.         errbox = MsgBox("Error Encountered", vbCritical)
    51.    End If
    52. End Sub

    It works pretty well. I have got it doing a 739 line file in 50 seconds.
    It works by line, not by size. There is a max line length though, so use files where the data is on seperate lines. Note: this only works for text based files - .txt, .html, etc

    I use it to offer an online backup service on a database program i'm working on, and it has had good response from my users. To get the file back into vb is much easier, just use an INet Control.

    Note on serverfilename syntax:
    whatever.txt - writes to the same folder as the uploader file

    whatever/whatever.txt - writes to one folder up from the upload file (don't need start forward slash.

    ../whatever.txt - writes to one folder below the uploader file.

    Thats it. I find it a very useful tool, and i hope you will too.
    Attached Files Attached Files
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  2. #2
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Uploading a File to A Server from VB

    It says missing upload_module.bas !! May i ask u why we need that script in the website ?Thanks

  3. #3

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Uploading a File to A Server from VB

    Yeah I know that, but upload_module.bas is not required to use it, it's just a module i deleted without removing it from the project first. Should still work though.

    The reason you need the script on your website is that, it allows you, using the Server.MapPath and TextStream objects, to write to a file on your server. The INet Control uses this by sending, through the get method, the neccessary info to write to the file - serverfile, append and writeline, to that file on your server.
    Last edited by kazar; Apr 12th, 2006 at 06:20 AM. Reason: error
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width