Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I had used the following code but it executes with error msg texted on catch.source file exists on drive F and i have a folder named as New in C:/Wamp/www/New where i want to upload the file for test.the code is as...
private void upload_Click(object sender, EventArgs e)
{
Upload(@"F:/Rahul.txt");
label2.Text = "Uploading successfully completed...";
}
public void Upload(string fileToUpload)
{

try
{
FileInfo toUplaod = new FileInfo(fileToUpload);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("http://127.0.0.1/New/" + toUplaod.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
//request.Credentials = new NetworkCredential("USERNAME","PASSWORD");
Stream ftpstream = request.GetRequestStream();
FileStream file = File.OpenRead(fileToUpload);
int length = 1024;
byte[] buffer = new byte[length];
int bytesRead = 0;

do
{
bytesRead = file.Read(buffer, 0 ,length);
ftpstream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
file.Close();
ftpstream.Close();


}
catch
{
MessageBox.Show("Error ! connecting server..itni jaldi nhi chalunga Sir g..");
}



}
Thank you....
Posted
Updated 30-Dec-12 23:31pm
v2
Comments
Sergey Alexandrovich Kryukov 31-Dec-12 2:14am    
Are you really running an HTTP server and service such a strange directory?
—SA
OriginalGriff 31-Dec-12 2:18am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Perhaps, if you take the comments out and tell us what problem you are getting it would help? At the moment all we have is a small chunk of simple code, and "It don't work" which is not helpful as errors messages go...
Use the "Improve question" widget to edit your question and provide better information.
@RahulTripathi17 31-Dec-12 5:34am    
thank u sir to guide me ...if i use Webclient class to upload a file on server how should i code for that.

1 solution

I cannot believe that not only you are running HTTP server on "http://localhost", on default port; and that your have a sub-directory named "/127.0.0.1". It's more likely that you do not quite understand the URI scheme; and you actual URL is "http://localhost/New".

Please see for more information: http://en.wikipedia.org/wiki/URI_scheme[^].

I also not quite sure that you are running any HTTP server at all. Did you install one? You never mentioned it, and I suspect you don't quite understand how it works. Sorry if I'm wrong about it.

Also, I want to note that there are no situations where using a hard-coded URL is useful. However, if this is just a preliminary test, it's probably OK.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900