Page 1 of 2 12 LastLast
Results 1 to 40 of 41

Thread: How can i make vb upload a file to my web server?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    How can i make vb upload a file to my web server?

    Hi guys,

    Is it possible and if so how do i go about doing it.

    To make it so when a user presses a command button it will upload the file 'favorites.txt' (which is located in the app directory) to my webserver?

    So the user presses command1 then it uploads the file to my web server like www.mywebsite.com/files/


    Thanks,

    Jamie

  2. #2

  3. #3
    New Member
    Join Date
    Jan 2010
    Posts
    4

    Re: How can i make vb upload a file to my web server?

    i use FtpPutFile API to do it,instead of ocx.
    if you have any questions about the ftp api ,just google it

    the below lists the ftp apis may be used.

    Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
    ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

    Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
    (ByVal hInternetSession As Long, ByVal sServerName As String, _
    ByVal nServerPort As Integer, ByVal sUsername As String, _
    ByVal sPassword As String, ByVal lService As Long, _
    ByVal lFlags As Long, ByVal lContext As Long) As Long

    Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
    (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
    ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
    ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Boolean

    Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
    (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
    ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Boolean

    Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" _
    (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean

    Public Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" _
    (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean

    Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

  4. #4
    Addicted Member
    Join Date
    Jun 2006
    Location
    Texas
    Posts
    164

    Re: How can i make vb upload a file to my web server?

    Try this works for me:

    With Inet1
    .URL = "ftp://Yoursite.com"
    .UserName = "YourUserName"
    .Password = "YourPassword"
    .Execute , "PUT C:\YourFile.txt /YourText.txt"
    End With

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    Hi guys, Hope some one can help me here.

    The code by mickeykelely works amazingly well. How ever it dont upload to a folder on my web server if it has a space.

    For example if i have the folder 'Games and music' it wont upload to it...
    BUT if i have the folder like this Gamesandmusic' it will upload the file..

    Does anyone know how i can make it so it uploads with spaces in the folder names...


    also how do i get this to work?

    .Execute , "PUT text1.text /YourText.txt" (It dont do the text1.text)

    Thanks
    Jamie

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    also how do i get this to work?

    .Execute , "PUT text1.text /YourText.txt" (It dont do the text1.text)


    Tou can't PUT data in Text1.Text. It must be a file.

    Try something like this:

    Code:
    Private Sub Command1_Click()
     Dim LocalPath As String
     Dim RemotePath As String
    
     LocalPath = Chr(34) & App.Path & "\Text1Data.txt" & Chr(34)
    
     '
     ' If you want to put the file in the root directory then just use filename only
     '
     RemotePath = Chr(34) & "path to directory/filename" & Chr(34) 
     
     With Inet1
       .AccessType = icDirect
       .Protocol = icFTP
       
       .UserName = "your username"
       .Password = "your password"
       .RemoteHost = "www.sitename.com" '<--- You may have to use ftp.sitename.com"
      
       .Execute .URL, "PUT " & LocalPath & " " & RemotePath
      
       While .StillExecuting
         DoEvents
       Wend
            
       .Execute , "CLOSE"
     End With
        
     MsgBox "Transfer Complete"
    End Sub
    Last edited by jmsrickland; Jun 3rd, 2011 at 12:40 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How can i make vb upload a file to my web server?

    Quote Originally Posted by JamieWarren09 View Post
    For example if i have the folder 'Games and music' it wont upload to it...
    BUT if i have the folder like this Gamesandmusic' it will upload the file..
    That is because INet control does not support spaces in folder/file names - it truncates text after the first space.

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How can i make vb upload a file to my web server?

    Perhaps something like:
    Code:
    .Execute , "PUT ""D:\Games and Music\fuddly.txt"" fuddly.txt"

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    That is because INet control does not support spaces in folder/file names

    Not if within quote marks. See post 6


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    Hui,

    Thanks for the replys. How ever is there Nyeay or another way I can upload to folders with spaces. It's just I have over 600 folders on my web server and I can't sit and rename everyone to remove the spaces..

    Thanks jamie

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    you can try using a shell object, i find it is much easier to work with than the inet control, can handle paths and filenames with spaces, can copy entire folders, with sub folders

    there are some examples in this forum, search on shell ftp,
    note files are copied asyncronously, so the code will continue without waiting for the files to finish copying
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    Hi Westconn, THanks for the reply.

    I found this code

    dim initdir as variant
    initdir = "ftp://user[email protected]/myfolder"
    Set sh = CreateObject("shell.application")
    Set n = sh.namespace(initdir)
    n.copyhere filepath\name ' can be file or folder

    which i think was provided by yourself in another post. How ever I am getting a error on this line..

    n.copyhere filepath\name ' can be file or folder



    I changed it to - n.copyhere C:\old\main\songs.son ' can be file or folder

    But it gives me compile error:
    Syntax error


    Any ideas how to correctly put in the file path?

    Thanks Jamie

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    string needs to be enclosed in quotes
    vb Code:
    1. n.copyhere "C:\old\main\songs.son" ' can be file or folder
    note if you use variable to pass filenames, must be of type variant
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    Hi Mate

    I tried that, it gives me the error

    Object variable or with block not set

    Thanks
    Jamie

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    If you run this:

    Code:
      '
      '
     Dim initdir As Variant
    
     initdir = "ftp://<--user-->:<--password-->@www.myserver.com.au/myfolder"
    
     Set sh = CreateObject("shell.application")
     Set n = sh.namespace(initdir)
    
     n.copyhere "C:\old\main\songs.son" ' can be file or folder
      '
      '
    I guarantee you it works. I know because I just ran it and it uploaded the file to my website.
    If you simply just can't get it to work then the one I posted in #6 works just as well.
    If you have problems then you are doing something wrong or it's your website or something else.
    Last edited by jmsrickland; Jun 3rd, 2011 at 08:41 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    Still not working, Is there any other way of uploading a file by ftp to folders with spaces?

    Thanks
    Jamie

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    Still not working

    That is not the way to get help. You need to work with us and tell us why it isn't working; where it isn't working and show the relavent code for support. No one here is going to be able to help you if you don't do this.

    I get the impression that you are not reading these posts. I know that #6 and #16 work for sure.

    The code works; you are just setting it up wrong

    Yes, there are other ways. Look at post #3. You can also use pure FTP; you can use Winsock, you can ShellExecute ftp.exe, and there are a few APIs that will do it.
    Last edited by jmsrickland; Jun 3rd, 2011 at 07:17 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    It's the same error :


    Object variable or with block not set

    Thanks
    Jamie

    Highlights the n.copyhere "C:\old\main\songs.son" ' can be file or folder line

    thanks
    Jamie.

  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    Post your code; not a type written copy of it. That statement works for me so your problem lies elsewhere. As a matter of fact. If your arguments are incorrect you will not receive any errors at all; it just wont do anything.
    Last edited by jmsrickland; Jun 3rd, 2011 at 07:43 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    the error indicates you are not logged into the ftp server or your path (initdir) contains a folder that does not exist on your server, eg myfolder is invalid or your username or password are incorrect, we can not fix those for you, nor can we provide examples specific to your ftp server

    if you want someone to test for you we would need to be able to login to your ftp server, with user name and password, which you may not want to post here, but you could pm to someone to help you
    you can test before the error
    vb Code:
    1. if n is nothing then msgbox "unable to login or folder invalid"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    Hey Guys, The error has now gone!

    However,

    Using the code above and with the right details, Nothing is happening now, it's not uploading anything to the server.. What is the cause of this?

    Cheers

    jamie

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    it does come up with the window saying calculating how long e.t.c

  24. #24
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How can i make vb upload a file to my web server?

    Maybe somebody hijacked your account and changed the password. FTP is great that way, sending your logon credentials in plain text.

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    No ones changed any of my details, besides i've just made another ftp account, same thing (no error message) but dont upload!

  26. #26
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    Uploading a file to a remote computer doesn't take rocket science. It's just as easy as copying a file from one directory to another on your own computer.

    Here's your problem(s).

    1) Your local path is incorrect
    2) Your remote path is incorrect
    3) Your username and/or password is incorrect
    4) Your ftp server doesn't allow data transfer
    5) Your initdir is incorrect
    6) Your computer has a firewall preventing I/O

    Either any one of the above or all of them

    Good luck.
    Last edited by jmsrickland; Jun 4th, 2011 at 11:59 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: How can i make vb upload a file to my web server?

    How am i not listening to you. if i hadent been listening to you, the error message would have not gone.

    Your saying if my arguments are incorrect nothing would happen, Yet westconn1 is saying that the error i got was cause the details to my server was incorrect.. My details are fine...

    I'm sorry if it seems i am not doing what you are saying.. But I am.

  28. #28
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    Your saying if my arguments are incorrect nothing would happen, Yet westconn1 is saying that the error i got was cause the details to my server was incorrect.. My details are fine...

    If your parameters are incorrect you will not get any errors from the shell.application side. Any errors you get are from VB because of syntax or run-time. If you run the code exactly as is you will not get any errors at all unles the error is caused from somewhere else in your code but it has nothing to do with your uploading a file.

    This is one of the simpliest ways to upload or download files from ftp sites.

    Now you say you are not getting any errors but nothing is being uploaded. You know this to be true because you used an FTP client to go to your FTP site and you are looking in the correct folder and did not see your files there? Only answer to this is one of your parameters are still incorrect or your ftp server is blocking the data transfer or you have a firewall blocking transfer. One thing for sure is that the code given to you is absolutely correct.

    If you can't get it to work either try some other approach or do as westconn suggested in his post #21. PM someone with you user name and password and allow that person to test for you using your ftp server and your project.
    Last edited by jmsrickland; Jun 4th, 2011 at 09:44 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  29. #29
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How can i make vb upload a file to my web server?

    I have a question , I was wondering if you could help me ...

    I want to make a program which uploads a text file to the server with
    internet control (with Username and Pass)

    I need the following features:

    -The file which is being uploaded is in a specific drive
    -I have a text box with specific text in it which i want it to be the
    name of uploaded file

    The problem with the code you posted is that i can not change the name of file ( /YourText.txt)
    to the text in textbox

  30. #30
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    this is an old thread, you should have started your own new thread
    you should tell us what code you are trying to use, ftp upload to server, and which line is the problem
    possibly try like
    Code:
    .Execute , "PUT C:\" & textbox.text & "  /" & textbox.text    , change c: to your drive and the name of the textbox to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  31. #31
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Question Re: How can i make vb upload a file to my web server?

    Quote Originally Posted by westconn1 View Post
    this is an old thread, you should have started your own new thread
    you should tell us what code you are trying to use, ftp upload to server, and which line is the problem
    possibly try like
    Code:
    .Execute , "PUT C:\" & textbox.text & "  /" & textbox.text    , change c: to your drive and the name of the textbox to suit

    Thanks for your guidance on posting new thread ...

    I know how to work with :
    Code:
    .Execute , "PUT C:\YourFile.txt /YourText.txt"
    The problem is that when the code is in "" , I can not use text in the textbox as uploaded file's name . Because the name will be TEXT1.TEXT! not the text in the textbox

    Thanks for you respond
    Last edited by GlowingVB; Feb 3rd, 2014 at 05:57 AM.

  32. #32
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    did you test what i suggested?
    the content of the textbox would be in the string, for both sourcefilename and destinationfilename
    to see what the string actually contains, try
    Code:
    msgbox "PUT C:\" & textbox.text & "  /" & textbox.text
    assuming you changed the name of the textbox to match the one you are using

    if your filenames can contain spaces, you will may have issues using inet control
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  33. #33
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How can i make vb upload a file to my web server?

    Thanks for your help , It works!

    Of course Just one of the Textbox.text is enough when the directory is specific , like this :

    .Execute , "PUT C:\YourFile.txt" & " /" & Text1.Text

    Text1.text = The name of file not the name of file in drive!

    Last edited by GlowingVB; Feb 3rd, 2014 at 06:44 AM.

  34. #34
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How can i make vb upload a file to my web server?

    There is something Strange !

    When The Directory is for example " PUT C:\Text1.txt" , It works! But when the directory is a little bit longer , it does not work!
    What is the reason of this ?

    Thanks a million ...

  35. #35
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can i make vb upload a file to my web server?

    The path length does have a limit

    What is the little bit longer path?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  36. #36
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    The name of file not the name of file in drive!
    this makes so much sense

    But when the directory is a little bit longer
    does the longer path contain spaces?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  37. #37
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How can i make vb upload a file to my web server?

    Something like this:

    C:\Program Files\New Folder\1.txt

    I do not think that it is that long !

  38. #38
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How can i make vb upload a file to my web server?

    program files and new folder contain spaces

    i am not sure if inet can work with paths with spaces, at the very least the path would have to be enclosed in " " s

    there is a disclaimer to the effect that inet can not work with spaces near the beginning of this thread, but i have not tested

    you could try to convert the long folder /filenames to their short path equivalent
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  39. #39
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How can i make vb upload a file to my web server?

    Anyway , thanks for your respond . If i find a way to solve this , I will let you know for sure

  40. #40
    Member Skittles's Avatar
    Join Date
    Feb 2011
    Location
    New Mexico, USA
    Posts
    42

    Re: How can i make vb upload a file to my web server?

    EDIT: I added the filename to the end of the RemotePath string. It did upload the file, but I still got state 11 = Error communicating. And, the file is empty

    I know I'm way late, but here I am...

    I get a serires of "States" from the iNet control. It seems like my computer and my website are talking fine, but then I get state 11 = "An error occurred in communicating with the host computer."

    My FTP software shows my path as "anadrac.com/fivecrowns". I've also tried "/anadrac.com/fivecrowns" and "anadrac.com/fivecrowns/"

    Like jmsrickland said, I must have something wrong.

    Thanks for any help,
    Joe

    Code:
     Dim LocalPath As String
     Dim RemotePath As String
    
     LocalPath = Chr(34) & App.Path & "\fcGameData.txt" & Chr(34)
    
     '
     ' If you want to put the file in the root directory then just use filename only
     '
     RemotePath = Chr(34) & "anadrac.com/fivecrowns" & Chr(34)
     
     With Inet1
       .AccessType = icDirect
       .Protocol = icFTP
       .URL = "ftp://anadrac.com"
       .UserName = "username"
       .Password = "password"
       .RemoteHost = "ftp.anadrac.com" '<--- You may have to use ftp.sitename.com"
      
       .Execute .URL, "PUT " & LocalPath & " " & RemotePath 
      
       While .StillExecuting
         DoEvents
       Wend
            
       .Execute , "CLOSE"
     End With
    Last edited by Skittles; Mar 26th, 2024 at 12:37 PM.

Page 1 of 2 12 LastLast

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