PDA

Click to See Complete Forum and Search --> : Uploading a File to A Server from VB


kazar
Apr 8th, 2006, 01:08 PM
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.

<%
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:

Dim filename As String
Dim serverfile As String
Dim aspuploaderurl As String
Dim lincount As Long
Dim urlstring As String
Dim i As Long
Dim append As String
Private Sub upload_file_Click()
filename = InputBox("Enter Filename")
serverfile = InputBox("Enter Server Path")
aspuploaderurl = InputBox("Enter Uploader File URL")

On Error GoTo skipfileread
Open filename For Input As #1
Do

Line Input #1, countstring
lincount = lincount + 1

Loop
Close #1
skipfileread:
Close
On Error Resume Next

Open filename For Input As #1


For i = 1 To lincount
status_label = Str(FormatNumber((i / lincount) * 100, 3)) + "%"
DoEvents
If i = 1 Then
append = "2"
Else
append = "8"
End If
On Error Resume Next
Line Input #1, writeline

urlstring = aspuploaderurl
urlstring = urlstring + "?" + "filename=" + serverfile + "&" + "append=" + append + "&" + "linewrite=" + writeline
uploadinet.OpenURL (urlstring)


Next i

Close #1
skipall:
If Err <> 0 Then
errbox = MsgBox("Error Encountered", vbCritical)
End If
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.

tony007
Apr 11th, 2006, 11:50 PM
It says missing upload_module.bas !! May i ask u why we need that script in the website ?Thanks

kazar
Apr 12th, 2006, 06:19 AM
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.