Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > c# > january 2010 >

c# : FTP Upload using FtpWebRequest problem PLEASE HELP


SevDer
11/20/2004 12:50:13 PM
Hi,

I am trying to write a simple application which copies the predefined
files (hardcoded in the code) to my ftp server.

I couldn't make it working. I don't want to use any 3rd party
components, because I want to learn. (I am using .NET Framework 2.0, but
I don't think my problem is because of 2.0)

########################################
My code fails at line:
Stream requestStream = myFtpRequest.GetRequestStream();

########################################
And the error message is as follows:
The remote server returned an error: (550) Requested action not taken.
File unavailable (e.g., file not found, no access)..

The file that I want to upload may or may not exist in the
destionation. (For testing I put it there also, but didn't work)

########################################
The method I am using is as follows (got it from MSDN help files)

public static bool UploadToFTP()
{
// The URI described by serverUri should use the ftp:// scheme.
// It contains the name of the file on the server.
// Example: ftp://contoso.com/someFile.txt.
//
// The fileName parameter identifies the file containing
the data to be uploaded.
Uri sevderComFtp = new
Uri("ftp://sevder.com/mysubfolder/myfiletoupload.ext");

string assemblyPath = @"C:\my folder\sub
folder\myfiletoupload.ext";

if (sevderComFtp.Scheme != Uri.UriSchemeFtp)
{
return false;
}

// Get the object used to communicate with the server.
FtpWebRequest myFtpRequest =
(FtpWebRequest)WebRequest.Create(sevderComFtp);
myFtpRequest.Method = FtpMethods.UploadFile;
myFtpRequest.Credentials = new
NetworkCredential("username", "password");

// Don't set a time limit for the operation to complete.
myFtpRequest.Timeout = System.Threading.Timeout.Infinite;

// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];

int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(assemblyPath);


Stream requestStream = myFtpRequest.GetRequestStream();

do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, bufferLength);
count += readBytes;
}
while (readBytes != 0);

Console.WriteLine("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the
request.
requestStream.Close();

FtpWebResponse response =
(FtpWebResponse)myFtpRequest.GetResponse();
Console.WriteLine("Upload status: {0}", response.Status);

response.Close();
return true;
}
########################################

--

SevDer
jamjar NO[at]SPAM gmail.com
12/16/2004 3:22:50 AM

[quoted text, click to view]

The only thing i can think of is that the FTP server has not logged in
before you are trying to request your file.

As a test try putting in
System.Threading.Sleep(5000);

before you start your Stream requestStream =
myFtpRequest.GetRequestStream();

Piotr Stulinski.
Aashish
1/12/2010 7:17:33 AM
I also had same problem, but I figured out, where is problem. I just used Fileupload.postedFile as Stream to read.

[Code]
string ftpfullpath = "ftp://" + ftphost + "/" + Path.GetFileName(inputfilepath);
//Create FTP request
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpfullpath));
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.Timeout = 600000;
ftp.ReadWriteTimeout = 600000;

ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftp.UsePassive = true;
ftp.UseBinary = true;
ftp.KeepAlive = false;

Stream requestStream = ftp.GetRequestStream();
const int bufferLength = 5120;
int readBytes = 0;
byte[] buffer = new byte[bufferLength];
do
{
readBytes = FileUpload.PostedFile.InputStream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
}
while (readBytes != 0);
requestStream.Close();

[Code]


From http://www.developmentnow.com/g/36_2004_11_0_0_18570/FTP-Upload-using-FtpWebRequest-problem-PLEASE-HELP.htm

Posted via DevelopmentNow.com Groups
AddThis Social Bookmark Button