Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with FTP uploading

Status
Not open for further replies.

davejam

Technical User
Jan 6, 2004
313
GB
Hi all,

I've been writing a program that simply streams the stock list from our website to a csv file on our users computer.

What i would like to do now is either take that stream and create a file on an ftp site, or download the stream to a file the copy the file to an ftp site.

I have scoured the web and tek-tips and came across many examples, many of which say they are not working (and after testing, didn't work for me either) I have added the latest couple i have tested to no avail...

using something simple get error
My.Computer.Network.UploadFile(Me.txtLocation.Text & "\myFile.csv", "ftp://MYFTP/myFile.csv")

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).


and as follows where webpagesource is the stream of data
Dim fileContents As Byte() = Encoding.UTF8.GetBytes(WebPageSourceCode)
Dim remoteFile As String = "ftp://IPAddress/myFile.csv"
Dim username As String = "user"
Dim password As String = "pass"

'Get the object used to communicate with the server.
Dim Request As System.Net.FtpWebRequest = FtpWebRequest.Create(remoteFile)

' Setting Properties
Request.Credentials = New NetworkCredential(username, password)
Request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Request.Proxy = Nothing
Request.KeepAlive = False

' Uploading file
Request.GetRequestStream.Write(fileContents, 0, fileContents.Length)
MsgBox("File Uploaded Successfully !!!")

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

Also tried
Dim up As New Net.WebClient
up.Credentials = New System.Net.NetworkCredential("user", "pass")
up.UploadFile("ftp://myFTP/myFile.csv", Me.txtLocation.Text & "\myFile.csv")

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

Is it something i'm doing wrong or should these work...

Thank you

daveJam

it works on my machine, so technically i win!
 
oh no.... school boy error!!!

I was testing our FTP using the external IP address, after looking at settings I tried local IP address and it worked with the following!!!

Dim up As New Net.WebClientup.Credentials = New System.Net.NetworkCredential("user", "pass")
up.UploadFile("ftp://myFTP/myFile.csv", Me.txtLocation.Text & "\myFile.csv")

which was the simplest to work out.... so just been getting errors with correct code!!!

just need to delete from the server and i'm laughing!!!

Cheers

daveJam

it works on my machine, so technically i win!
 
i've decided to go with the following code as it means i don't have to copy to a specific file, i can just stream to variable, then stream bytes up to server... but have 1 problem.

It doesn't close the connection so hangs open for 2 minutes before being forced to close by the server, just need to work out how to close connection (probably simple, we'll see) and post results back here just incase others have had the fun of FTP uploading.

Dim myContent As Byte() = Encoding.UTF8.GetBytes(WPSource)
Dim myFile As String = "ftp://IPAddress/myFile.csv"
Dim myUser As String = "user"
Dim myPass As String = "pass"
Dim myReq As FtpWebRequest = FtpWebRequest.Create(myFile)
myReq.Credentials = New NetworkCredential(myUser, myPass)
myReq.Method = WebRequestMethods.Ftp.UploadFile
myReq.Proxy = Nothing
myReq.KeepAlive = False
myReq.GetRequestStream.Write(myContent, 0, myContent.Length)



daveJam

it works on my machine, so technically i win!
 
aaaah....

myReq.abort

daveJam

it works on my machine, so technically i win!
 
Just realised this does not work as not all of the file is transferred...

Also looking at our ftp log it doesn't do a nice transfer complete nor does it sign out correctly, it just disconnects.

So i'm now stuck on the same issue.

Any ideas

Cheers

daveJam

it works on my machine, so technically i win!
 
right... think i've sussed it... i know i'm the only one asking the question and giving my own answers but struggled to find any help on this subject so hopefully my trial and error approach may help fill out the blanks in the subject...

removed line
myReq.GetRequestStream.Write(myContent, 0, myContent.Length)

instead, I set an object to do the write which means i can close and dispose of it when done, this gets a 'Tranfer OK' status on the FTP server log so i'm thinking all is good.

Dim myStream As Stream = myReq.GetRequestStream()
myStream.Write(myContent, 0, myContent.Length)
myStream.Close()
myStream.Dispose()

Cheers

daveJam

it works on my machine, so technically i win!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top