I am creating an application in C#
with FileSystemWatcher
, I am trying to upload multiple files to the servidor web
viaFTP
The idea is the following:
I have created a folder C://
where users copiaran,moveran y renombraran
files. I will have to order these files and rename them, to later upload them to the server.
The code is the following:
This function is responsible for uploading the files to the server:
/*ftp data*/
private String servidor = ConfigurationManager.AppSettings["servidorFTP"];
private String usuario = ConfigurationManager.AppSettings["usuarioFTP"];
private String clave = ConfigurationManager.AppSettings["claveFTP"];
private String puerto = ConfigurationManager.AppSettings["puertoFTP"];
private void uploadFile(String fileName, String fileFullPath){
//preparo la direccion donde se almacenara el archivo en el servidor
string url = servidor + fileName;
var ftpWebRequest = WebRequest.Create(url) as FtpWebRequest;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
//me logeo en el servidor
ftpWebRequest.Credentials = new NetworkCredential(usuario, clave);
byte[] fileData = GetFileData(@fileFullPath);
using (var requestStream = ftpWebRequest.GetRequestStream())
{
requestStream.Write(fileData, 0, fileData.Length);
}
var response = ftpWebRequest.GetResponse() as FtpWebResponse;
}
private byte[] GetFileData(String filename){
using (var sr = new StreamReader(filename))
{
return ASCIIEncoding.ASCII.GetBytes(sr.ReadToEnd());
}
}
This code works fine if it is a single file, but when I try to upload several at the same time the code does not work correctly.
Also if I run the function to move the document to the new directory. This crashes as moving files locally runs faster than uploading.
code to move:
private void moverArchivo(String archivo){
System.IO.File.Move(carpetaWatch + "\\" + archivo, carpetaWatch + "\\" + anoActual + "\\" + archivo);
}
FileSystemWatcher only works for you with local environments, not remote. When you work with FTP. From the server side you never know when the file stopped being manipulated, only that there was an affectation in the directory, which may be that the file was created and is starting to be transferred; you will not receive a notification from systemwatcher when the file is no longer modified.
In a company that I work for they had work with FTPs. To indicate that the file has finished uploading, they added a "semaphore" file, which is an empty file with the same name, and an s suffix. Example: "file.zip" and "file_s". This allowed a console application to move only those files that had been explicitly marked as successfully transferred.
An FTP connection can fail, you will never know if the file is complete. FTP is a protocol prone to many external bugs. You can generate a read strategy, if a file is N minutes old and does not contain a semaphore file. Sure, something went wrong in the transfer. If the file contains its semaphore, the transfer was successful, and finally, if the file was created, hasn't timed out, and has no semaphore, then the file is being worked on.