Results 1 to 25 of 25

Thread: Automatically uploading a file to a server

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Automatically uploading a file to a server

    I'm new to server management. 1) What is the VB code to automatically upload a file, such as the results of a test, to a web server online? 2) What must I do and have on my web server to get this to work?

    Here's some code I found that I think will do the trick. I want the user to click a Submit button so it submits the results of a quiz to the WEb server:

    Will this work in 6.0? From: http://support.softartisans.com/docs...g_simplevb.htm

    A Simple Visual Basic Upload

    To use XFile from Visual Basic, add XFile to your list of VB references.
    Exercise 3: A Simple Visual Basic Upload

    1. Create a new, standard, Visual Basic project.

    2. Open the Project menu, select References, and check "SAXFile 1.0 Type Library".

    3. Create a command button on your form, name it cmdSendFiles, and give it an appropriate caption (such as, "Upload").

    4. Enter the following code in the code section of your form.


    Private Sub cmdSendFiles_Click()

    '--- Create an instance of the XFRequest object, XFile's principal object
    Set XFile = New XFRequest

    '--- Set the Server to which you are uploading
    XFile.Server = "localhost"

    '--- Set the specific script to which you are uploading
    XFile.ObjectName = "/SAXFileSamples/asp/post/onefile/formresp.asp"

    '--- Set a file to upload
    XFile.AddFile "C:\boot.ini", "File1"

    '--- Begin transferring the files
    XFile.Start

    '--- Destroy the XFRequest Object
    Set XFile = Nothing

    End Sub

    5. Run the project and click on the upload button. Boot.ini will be uploaded to the local server.

    Understanding the Script

    * Set XFile = New XFRequest creates an instance of the XFRequest object. XFRequest is XFile's principal object. XFRequest sends the HTTP request (Get, Post, or Put) to the server.

    * SAXFile.Server sets the domain to which the file will be posted. In this example we use "localhost." If we were posting through the Internet, we would use the format "www.domain.com."

    * SAXFile.ObjectName specifies the exact page to which our request will be posted. In this case, it is formresp.asp, located in /saxfilesamples/asp/post/onefile. Formresp.asp uses SoftArtisans FileUp to process the upload.

    * SAXFile.AddFile presets a file to upload. Addfile takes two parameters: the path and file name on the client, and an item name for reference to the file on the server.

    * SAXFile.Start initiates the file transfer.

    * Set XFile = Nothing destroys the project's instance of the XFRequest object. This prevents draining of your resources.

  2. #2
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439

    Re: Automatically uploading a file to a server

    It does not sound like this will work for you. This uploads a file to a server, what you need is to send data to the server, not a whole file.

    There are two ways you can do this.

    The first, is to develop a server-side page on the server to accept the results (with ASP, PHP, ColdFustion, Perl, etc). Then you would develop an application to connect with the server and pass the quiz results.

    The second would be to develop both a server application and a quiz application. This would probably be more messy for what you want, however.

    Do you have any experience with the Winsock control?
    "I don't want to live alone until I'm married" - M.M.R.P

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    I do not have any winsock experience, but I like that code and will design a test result viewer to view results. Thanks.

  4. #4
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    Ive attached a program i made to this thread, download it, extract the files to somewhere, and load up the project.. Its a very simple winsock tutorial i made, each line is noted to let you know what it does.

    When you have it on yourside, compile it, and run the compiled code, then run it again from VB or again from the .EXE you compiled.

    After there both running, click the 'Server' button on 1, and click 'Connect' on the other.

    I kept it very simple, but it works. If you have any questions please let me know.
    Attached Files Attached Files

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    I found the Winsock tutorial very simple but there are no upload procedures, so I found one on this forum: http://www.vbforums.com/showthread.p...72#post2775272

  6. #6
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    Quote Originally Posted by kelceycoe
    I do not have any winsock experience, but I like that code and will design a test result viewer to view results. Thanks.
    My code was simply for that matter there, but, you might wanna try the code from the first post on this thread http://www.vbforums.com/showthread.php?t=377648

    Its much simpler then the code you got from the other thread.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    Can't get it to upload. The folder I'm uploading to is set to 666 or 644, is there any reason its not sending data?
    Last edited by kelceycoe; Feb 8th, 2007 at 03:07 PM.

  8. #8
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439

    Re: Automatically uploading a file to a server

    If the permissions is 644 then it won't be able to write the file to the directory. Will need to be at least 666. Check that first.
    "I don't want to live alone until I'm married" - M.M.R.P

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    Ok tried that and my folder is now on 766 but i still cant get it to work. Here's my remote connection:

    domain/folder

    Is this alright?

    My server is Unix, does that help?

  10. #10
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    Quote Originally Posted by kelceycoe
    Ok tried that and my folder is now on 766 but i still cant get it to work. Here's my remote connection:

    domain/folder

    Is this alright?

    My server is Unix, does that help?

    Looks like your trying to send a file from the client to the server or vice-versa, but when you send the file, it looks like your sending it to the IP(or domain) with the path included.. If you do that you need to parse it out on the receiving side.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    How do I do that? I'm trying to send it from client to server. Thanks for all your help.

  12. #12
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    you gotta send the other computer signals, or markers so that it knows what to do with the incoming information, you could also use multiple winsock controls doing this. Sometimes you'll run into problems and have to use multiple winsock controls, but for what im about to tell you, you only need to do it with one.

    In your winsock control under DataArrival, you do your command to get the data, and put it in a string.

    Then use Select Case to check the first character or 2 or 3 or however many you want, depending on how much is going into your program..

    Those first characters are your markers, use Left$, Right$, and/or Mid$ to pull out the markers, and use Select Case to check what it is. And in your case, the first data you send will have a marker noting that its the path to put the file, and then the 2nd packet will be your filename(could include that in your first if you want), and 3+ packets would be the data for the file.

    So on the client side, you can do

    .SendData "[Path]C:\MyDownload\"
    .SendData "[File]MyFile.dat"
    .SendData "[Data]"

    and from there start sending the data. Since all the markers are 4 letter words with 2 brackets, you would check like this:
    Left(YourData, 6)

    Thats the code to pull out only the left 6.. If you cant figure it out and you need more specific code, post it here and i'll go deeper

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    I'll just send you my code... if you go to KelceyCoe.com you'll find my email address. Send me an email so I know its you and then ill send you my project files. Thanks.

  14. #14
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    I sent you an email

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    I sent you an email too... did you not get it? Ill send it from my hotmail account.

  16. #16
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    i got it, working on it now

  17. #17
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    frmMain
    txtRemoteCon.text : You can remove this, and the label for it. Since your program will most likely send to the same place each time. If it changes, hide it and use it to store the IP address or something. Dont use your .com address, you need to use the IP address.

    txtRemotePort.text: Same goes with this, should be the same always, so you can just type in the code instead of having an extra useless control in your program.

    for your SendFile sub, thats where you will have the host and port. For RemoteHost, only use the IP address, for RemotePort, just type in the value yourself.

    txtFileName: You probably dont really need this either, and you probably dont even need to save it as a file on your computer unless you need to save the record of it to view it later on, on that same computer. Instead just dont unload the forms that have the info, hide them with FormName.Hide. Thats upto you though, sometimes using a file is much easier.

    Under Send File is where you'll do all the work before sending the actual data. You'll do something like

    VB Code:
    1. SckSendFile.SendData "[File]" & SFileName
    2. SckSendFile.SendData "[Size]" & CStr(LOF(iFileNum))

    Path and everything else the server should already know, if not then
    VB Code:
    1. SckSendFile.SendData "[Path]" & strPathName  'or whatever the path variable might be


    On the receiving end, the server should be setup to check the first 6 of the incoming data, like this:

    VB Code:
    1. Select Case Left(Data, 6)
    2.     Case Is = "[File]"  'This is the filename
    3.         Data = Right(Data, Len(Data) - 6)
    4.     Case Is = "[Size]"  'This is the size of the file
    5.         Data = Right(Data, Len(Data) - 6)
    6.     Case Is = "[Path]"  'This is the directory to put the file in
    7.         Data = Right(Data, Len(Data) - 6)
    8.     Case Else   'This is the actual file, if the incoming data begins with anything else, its data.
    9. End Select

    Under Case Else is where all the code for receiving the file needs to go..


    My only question is... This is looking like your trying to upload it to a web server. Do you have the domain being forwarded to your IP address and the server is your home computer? Or are you actually uploading this to another server where you cant put your own code?

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    Im actually sending to a web server I pay for. Its at hostgator.com

    Can you just code the program for me? I'm not good at advanced stuff and I need this program badly. Thanks.
    Last edited by kelceycoe; Feb 8th, 2007 at 08:57 PM.

  19. #19
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    I cant code this for you for 2 reasons.

    1) I dont know anything about your web server or how you login or the username and pass or anything, becuase you have to have that to do this.

    and 2) Isnt this homework?

    I can help you with code and sample code, and i can help point you in the right direction, but actually finishing it for you i cant do. Im not sure anybody here will do that, but some people may try =)

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    This isn't homework. This is for personal reasons.

  21. #21
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Re: Automatically uploading a file to a server

    Ok, for this you gotta make a whole seperate program just to play around with.

    Your gonna want to setup the winsock control to connect to your server, and somewhere on your form have it display the .GetData

    That way you can figure out exactly what needs to be sent and when, most likely you'll get encoded strings back which are gonna be hard to figure out how to get through, but since its a webserver it might just be short bursts of HTML requesting information like username, password, things like that..

    Ive never done this part before successfully, but this is how i got my start on it, and i did get somewhere.. Play around with that, and maybe start up a new thread to see if anybody knows how to upload to a web server.

    Since you cant just throw a server program you made on there, you gotta connect and upload the way the server expects you too..

  22. #22
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439

    Re: Automatically uploading a file to a server

    Quick question... does this server support ASP coding? If so, why not just write an ASP page and have your client program submit [POST] the file to an ASP script which uploads it?

    I have an example program for connecting to a webserver and retrieving a webpage in this thread: http://www.vbforums.com/showthread.p...23#post2317923

    You'll need a bit more headers information because you want to POST form information (technically, at least). If you don't know how to do that then I'll look into it. (I know basically how it's done, but haven't yet done it.)
    "I don't want to live alone until I'm married" - M.M.R.P

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    Yea the server supports asp coding. I will look at your program and tell you how it works. Thanks.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    16

    Re: Automatically uploading a file to a server

    How can a program meant to download help me in uploading? I know nothing about uploading or downloading. I am going to contact my server host for further details with the other script but look forward to further replies from you guys. Thanks.

  25. #25
    Hyperactive Member Disiance's Avatar
    Join Date
    Sep 2004
    Location
    Denver, CO
    Posts
    439

    Re: Automatically uploading a file to a server

    Because when you upload a file, you end up downloading a webpage in response. When you upload a file, you would give additional HTTP headers telling the server what you're uploading, and then it would give you a response.
    "I don't want to live alone until I'm married" - M.M.R.P

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