Tak for svaret, men jeg har nu kigget uden held. Det kan selvfølgelig være at det er fordi jeg ikke umiddelbart kan se hvordan jeg skal kæde det sammen.
Ip-adressen er ikke et problem, men få den sat ind i et HTML dokument og derefter få det uploadet til min udbyder volder mig alvorlige problemer. Jeg kunne jo selvfølgelig bare gøre det manuelt, men..........
Du kan i hvert fald finde din ip med winsock1.localip. At generere et html dokument er ikke så svært. Åben en fil for output og skriv så html koden i den. Når du siger uploade, hvad mener du så, at sende den til en ftp server eller hvad?
Nu skriver du at du har problemer med det med html. Et eksempel: Open "IP.htm" for output as #1 write #1, "<html><body bgcolor=....." write #1, "Min IP er: & winsock1.localIP write #1, "</body></html>"
Vel. Jeg kommer selv til at skulle bruge en lign. funktion inden så længe, så jeg bikser lige en kontrol sammen der kan uploade(og for den sags skyld downloade). Hvis jeg lige får din e-mail sender jeg den til dig så snart den er færdig. Det regner jeg med at den er inden i morgen aften.
Nå, der kom jeg vidst til at love lidt for meget. Jeg er bange for at jeg ikke kan nå at lave en sådan kontrol til i morgen, da jeg først skal være færdig med en udbygget list control. Prøv i stedet at se på classen på: http://216.5.163.42/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=5656
Den kan godt nok kun uploade filer, men i første omgang har du vel heller ikke brug for andet.
webster > meget brugbar kode, men lige for at være lidt besværlig - jeg skulle meget gerne uploade filen til et bestemt bibliotek på serveren og det syntes der ikke lige umiddelbart at være mulighed for ?
Kigger på det så snart jeg kommer hjem. Overload.dk > det skal du ikke være så sikker på. Jeg er bare blevet helt vild med at lave controller på det sidste.
Nå. Som altid gør jeg noget andet end jeg siger. Jeg kom lige i tanke om, at der egentligt ikke er nogen grund til at lave en control der allerede findes. Nemlig Microsoft Internet Transfer Control. Med den skal man "bare" kunne en lille smule Ftp kommandoer, så glider alt. Jeg har prøvet at lave et eksempel. Jeg har ikke VB her på skolen, så der er ingen garanti for at det virker: Public Sub SendFiles(ByVal Brugernavn As String, ByVal Password As String, ByVal ServerAdress As String, ByVal FilSti As String, ByVal ServerBibliotek As String)
'Disse to linier stopper eventuelle operationer, og lukker en evt. eksisterende forbindelse Inet1.Cancel Inet1.Execute , "CLOSE"
Alladvantage siger mig ikke lige så meget, men her er lidt mere om FTP med InetKontrollen i tilfælde af at du ikke har fundet ud af det endnu (Ved godt det er lidt meget at poste).
Introduction
Well, the final part of the Inet series has finally arrived! This week, I will take you through looking at using the Inet control to perform FTP operations. We will look at connecting to an FTP server, getting directory and file listings, removing files on the server, uploading and downloading files and other FTP tasks. At the end of this article you will also see a downloadable project that contains all the code from this article, as well as a class module that includes all the API declares for the Inet FTP commands (ie you don't need to control)
Get connected
Lets start off with the basics first: connecting.
The first thing we need to do is to set the Protocol property of the Inet control to icFTP. We also need to specify a server address (in the form of a URL) and a username and password to login. If the FTP site doesn't require a username and password, then you can just leave them blank. All of these properties can be set using the Property Page in the IDE, or you can set them at run time:
With Inet1 .Cancel .Protocol = icFTP .URL = "localhost" .Username = "" .Password = "" End With
You may have noticed that there is no specific Connect command in the Inet control, so what we have to do is use the Execute command, which allows us to execute a special FTP command.
(If you have ever used DOS before then you will be familiar with some of the commands used)
Your wish is my command
Just think back to the last time that you used an FTP program. What is the first thing it does after connecting? Show a list of all the directories and files in the default directory. This can be done using a DIR command and some clever stuff in the Inet's StateChanged event.
Here are some of the commands that you will be using when you FTP into a server:
CD - Change directory CDUP - Change to the parent directory. The same as "CD .." DELETE - Deltes a file DIR - Retrieves the current directory listing GET - Retrieves a file MKDIR - Makes a new directory PUT - Uploads a file PWD - Sends a password QUIT - Ends an FTP session RECV - The same as GET RENAME - Renames a file RMDIR - Deletes a directory SEND - Same as PUT SIZE - Retrieves the size of a file
This is all very well and true, but how do I actually connect and get the contents of the root directory? Lets find out.
Navigating the server
Well, in the Inet's StateChanged event, we need to monitor when data comes in and then do some trickery to get what the server is sending. Firstly, it is a good idea to store the current command (e.g. GET) as a module level variable. This way all the procedures can access it and we know what we are currently doing.
For example, if we wanted a list of directories, then we would set our variable to DIR, and then use the Execute method. This way when we are in the StateChanged event, a completely different procedure, we still know what the last command was.
The StateChanged event procedure provides us with one parameter, State. We can use a Select Case statement to determine what is currently going on. Now, the server only sends back data after the state of icResponseCompleted, so we need to stick in another Select Case statement to find out what command we are executing.
Lets quickly recap on the code. Here is what we have so far in the StateChanged event:
Private Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State Case icResponseCompleted Select Case strCmd Case "DIR" End Select End Select End Sub
OK. As the StateChanged event only passes the State parameter, we need to manually get the data that is being sent by the server. We do this using the GetChunk method. Because of the way things are, we can only get 512 characters at a time, so we need to set-up a loop that will make sure that we get all the data. It looks something like this:
Do strNewData = Inet1.GetChunk(512,icString) DoEvents If Len(strData) = 0 Then Exit Do strData = strData & strNewData Loop
Notice the use of DoEvents. This is very important because we must give the server time to sort itself out otherwise we will run into a whole load of problems.
If you are currently testing out this code, you will notice that the contents of strData are not exactly 'user friendly' at the moment. We still need to go through and format the data, for whatever control we are going to load it into. Each file and directory is in the string and at the end of each one, a vbCrlf character can be found. This makes it easy to parse out all the different items using some of VBs great string functions:
Do While Len(strData) intPos = Instr(strData,vbCrlf) If intPos Then strItem = Left$(strData, intPos - 1) strData = Mid$(strData, intPos + 2) Else strItem = strData strData = "" End If Loop
But how do we know the difference between a file and a directory? Well, fortunately all directories come with a trailing /, so we can look for that when we want to do something with the item:
strItem = lstDir.List(lstDir.ListIndex) If strItem = ".." Or Right$(strItem,1) = "/" Then DoEvents If strItem = ".." Then strCmd = "CDUP" Inet1.Execute ,strCmd Else strCmd = "CD " & Left$(strItem, Len(strItem) -1) Inet1.Execute ,strCmd End If End If
OK. So now we have a list of directories. The next thing we are going to learn, is how to upload files.
Files & Directories
This starts to get a bit simpler, because it is mainly a case of using the Execute command.
The syntax of the PUT command is as follows:
PUT localfile, remotefile
Here is a sample:
Inet1.Execute ,"PUT c:\myfile.txt /myfile.txt" (Remember to include that preceeding '/')
To download a file, use the GET command:
GET remotefile, localfile Inet1.Execute ,"GET myfile.txt c:\myfile.txt"
To delete a file, you need to use the DELETE command:
OK. Now that we have finsihed with files, lets move onto directories.
When handling directories, we need to add and delete them. For DOS gurus, this should be like the good old days! Simply use the MKDIR and RMDIR to make and remove directories. You should be aware of the syntax for both of these commands:
Pretty simple, but you must remember to place the / in at the correct places, otherwise you will have problems.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.