473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

FTP Upload using FtpWebRequest problem PLEASE HELP

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
http://www.sevder.com
Nov 16 '05 #1
1 9430

SevDer wrote:
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
http://www.sevder.com


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.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy | last post by:
Hello, I have a web application where one of the admin features is to print a list of foreclosure listings. The client wants to have two cases per page printed. I have figured a way to insert...
16
by: t_pantel | last post by:
I 've got the following structure: typedef struct GROUPED { short val ; short code; short group; short forecast_cd; short double_ind; short min;
2
by: ywchan | last post by:
i use pop3 with .NET to retrieve email it works fine for english only...if the message contains chinese, the chinese characters sometimes display as base64 (e.g. =?big5?B?jkfajfdfjkk==?= ) /...
9
by: Tris | last post by:
Hi People I need help!!! Have got a excel file that i have put into an access file so that i can create a mailing carrier sheet without re typing everybodys details in. The excel file has...
6
by: gauravkhanna | last post by:
import java.io.*; class Automation1 implements Runnable { int iterationCount=0; Thread t; FileReader fr; //FileWriter fw; BufferedReader br; ...
2
by: abhishek11 | last post by:
Hi All I have a box with shadow around it. Now i have to render a red bar inside it at the top. This red bar should expand when the box expands but this is not happening in mozilla. Here is the...
2
by: shapper | last post by:
Hello, I am for days trying to apply a XSL transformation to a XML file and display the result in a the browser. I am using Asp.Net 2.0. Please, could someone post just a simple code example,...
3
by: james121285 | last post by:
I have been trying this program for ages and am not getting very far. I am trying to input data from a seperate file and use it to work out the max and min values of the data. I have done the second...
2
by: Ronald S. Cook | last post by:
Hi, Some users on our domain can run our WCF application no problem. Some get an unhandled exception error re: SOAP security negotiation. I'm wanting the service to not be secure.. I just want...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.