If the server will download, implement the methods:
public ActionResult DownloadFile()
{
FtpWebRequest objFTP = null;
try
{
objFTP = FTPDetail("Arquivo.txt");
objFTP.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse response = (FtpWebResponse)objFTP.GetResponse())
{
using (Stream ftpStream = response.GetResponseStream())
{
int contentLen;
// Não precisa usar necessariamente uma variável.
// Pode ser uma String fixa.
using (FileStream fs = new FileStream(variavelQueApontaOndeOArquivoVaiserSalvo, FileMode.OpenOrCreate))
{
byte[] buff = new byte[2048];
contentLen = ftpStream.Read(buff, 0, buff.Length);
while (contentLen != 0)
{
fs.Write(buff, 0, buff.Length);
contentLen = ftpStream.Read(buff, 0, buff.Length);
}
objFTP = null;
}
}
}
// Aqui você retorna uma View de Sucesso, seu lá como você quer fazer.
return View();
}
catch (Exception Ex)
{
if (objFTP!= null)
{
objFTP.Abort();
}
throw Ex;
}
}
private FtpWebRequest FTPDetail(string FileName)
{
string uri = "";
string serverIp = "255.255.255.1";
string Username = "usuario";
string Password = "teste123";
uri = "ftp://" + serverIp + "/root/" + FileName;
FtpWebRequest objFTP;
objFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
objFTP.Credentials = new NetworkCredential(Username, Password);
objFTP.UsePassive = true;
objFTP.KeepAlive = false;
objFTP.Proxy = null;
objFTP.UseBinary = false;
objFTP.Timeout = 90000;
return objFTP;
}
Now, if the server is going to send the files to an FTP server, implement the following:
[HttpPost]
public ActionResult Create(HttpPostedFileBase file)
{
if (file == null)
ModelState.AddModelError("Arquivo", "Arquivo deve ser válido.");
else
{
string nomeArquivo = DateTime.Now.ToLongTimeString().Replace("/", "").Replace(":", "") + file.FileName.Substring(file.FileName.LastIndexOf("."));
string path = Path.Combine(Server.MapPath("~/arquivos"),
Path.GetFileName(nomeArquivo));
file.SaveAs(path);
}
FtpWebRequest objFTP= null;
try
{
objFTP= FTPDetail(path);
objFTP.Method = WebRequestMethods.Ftp.UploadFile;
using (FileStream fs = File.OpenRead(CompletePath))
{
byte[] buff = new byte[fs.Length];
using (Stream strm = objFTP.GetRequestStream())
{
contentLen = fs.Read(buff, 0, buff.Length);
while (contentLen != 0)
{
strm.Write(buff, 0, buff.Length);
contentLen = fs.Read(buff, 0, buff.Length);
}
objFTP = null;
}
}
return true;
}
catch (Exception Ex)
{
if (objFTP!= null)
{
objFTP.Abort();
}
throw Ex;
}
return View();
}
Ideally X would provide a
Action
ofController
be aFileResult
, and Y has anotherAction
using aHttpClient
. I’ll think of something.– Leonel Sanches da Silva
The problem is that Y cannot have any application. It would only provide a directory.
– Diego Zanardo
the directory is accessible by explorer? for example servor2 Docs, already tried to use only
File.WriteAllBytes("\\servidor2\docs\teste.txt")
?;– Leonardo Bosquett
In web Forms I could do like this: string ftpAddres = "ftp://" + username + ":" + ftpPass + "@" + ftpserver + "/" + name; using (var Webclient = new System.Net.Webclient()) { Webclient.Proxy = null; Webclient.Uploaddata(new Uri(ftpAddres), file.Filebytes); }
– Diego Zanardo
You need to necessarily access an FTP directory?
– Leonel Sanches da Silva
Yes. Because it is an external server (UOL)
– Diego Zanardo