C# FTP Uload Acces denied Error 550 - c#

My Class FTP work fine wtih my FTP server, but not with my client FTP server.
public class UploadToFTP
{
public void UploadFTP(string LeSource, string Desti, string CodeClient)
{
Pers_Conf oConf = LeConf.Get_Config(CodeClient);
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + oConf.FtpServer + oConf.FtpChemin +"/" + Desti);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(oConf.FtpLogin, oConf.FtpPwd);
request.UseBinary = true;
request.UsePassive = true;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(LeSource);
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
this line is not work : Stream requestStream = request.GetRequestStream();
Anybody have solution ?
i have change the right into 777.
Thanks you in advance,
Stev

The Solutions
/ work only several ftp servers, but // it works perfectly everywhere

Related

C#: ftp upload is successful but dowloaded file is damaged

I'm using the code bellow to upload a zip file to to my ftp server:
string zipPath = #"d:\files\start.zip";
string ftpPath = ("ftp://######/start.zip");
WebRequest request = WebRequest.Create(ftpPath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("######", "######");
StreamReader sourceStream = new StreamReader(zipPath);
byte[] fileContents = Encoding.Unicode.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
try
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse makeFileUploadResponse = (FtpWebResponse)request.GetResponse();
}
catch
{
MessageBox.Show("ftp failed!");
}
My zip archive is definitely valid (I can open it and extract it) but when I download the uploaded zip file, I got the error that archive is damaged.
Update 1: my source code is from MSDN article:How to: Upload Files with FTP
you should cast request to FtpWebRequest (as in that MSDN example)
and then specify request as binary (you're uploading binary file, not text).
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("aa");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
I can't say what the problem is but I can provide an alternate solution. You can use WebClient instead of WebRequest
string zipPath = #"d:\files\start.zip";
string ftpPath = ("ftp://######/start.zip");
WebClient ftpClient = new WebClient();
ftpClient.Credentials = new NetworkCredential("####", "######");
try{
ftpClient.UploadFile(ftpPath, WebRequestMethods.Ftp.AppendFile, zipPath);
}
catch(WebException ex){
MessageBox.Show("ftp failed");
}

c# Why does FTP upload only succeed sometimes?

I'm working on a project, that collects some informations to a .CSV file and then send the file to a ftp server.
The program sends the file successfully sometimes, but other times it just stops and throws the error:
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: 227 Entering Passive Mode (192,168,10,170,216,244)
Can anyone spot anything, that I have done wrong?
Thanks in advance.
Here is my code
private void sendFile_Click(object sender, EventArgs e)
{
Upload("ftp://100.64.44.12", "UsernameHere", "PasswordHere", #"C:\Users\Kasper\Documents\testFolder\data.csv");
}
public void Upload(string FTPAddress, string username, string password, string filePath)
{
FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" +
Path.GetFileName(filePath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = false;
request.KeepAlive = true;
StreamReader sourceStream = new StreamReader("testfile.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
MessageBox.Show("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
Try setting FtpWebRequest.UsePassive = false

difference between uploadfile and uploadfilewithuniquename in WebRequestMethods.Ftp

Two questions here :
1)difference between WebRequestMethods.Ftp.uploadfile and WebRequestMethods.Ftp.uploadfilewithuniquename?
2)When i do an upload file using the code below for an already existing file would it override the file.And is it safe to assume that it would always override?
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;
// what if i use request.Method = WebRequestMethods.Ftp.UploadFilewithuniquename;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe#contoso.com");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
These methods refer to the FTP commands STOR and STOU.
If the logged in user has the privs, then STOR (WebRequestMethods.Ftp.UploadFile) will create a new file or overwrite an existing file.

How to send an excel file to FTP using C#?

I using the following code to send an excel file to FTP. File is sending, File size is also same. But file contains only spaces.
ftpAddress = "X.X.X.X";
outFilePath = "MyFolder/Sample.xls";
inFilePath = "D:/Hello.xls";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpAddress + "/" + outFilePath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(userId, password);
//FileStream stream = File.OpenRead(inFilePath);
byte[] fileContents = File.ReadAllBytes(inFilePath);
//byte[] buffer = new byte[stream.Length];
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
//Shows confirm message
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine(response);
response.Close();
Please help. Thanks in advance.
Source: http://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe#contoso.com");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
}
}

Uploading a WebImage to FTP

I am using ASP.Net MVC 4 and WebImage helpers. I need to upload my WebImage object via FTP. I just don't know how to get the WebImage parameters to send to my RequestStream.Write
Here is some code...
WebImage photo = WebImage.GetImageFromRequest();
string fileName = System.IO.Path.GetFileName(photo.FileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPaddress +"/images/" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("****", "*****");
request.UsePassive = true;
request.ContentLength = ???????;
byte[] buffer = ????????; //It's not photo.GetBytes;
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, request.ContentLength);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
And will I be able to use the photo.Save() to the FTP path afterwards?
byte [] fileContents = photo.GetBytes();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
That's how you do it, silly me.

Categories