1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace Module_FTP
{
public class FTP
{
public string Server { get; set; }
public string Username { get; set; }
public string Password { get; set; }
// parametre : nom du fichier sur le dossier local, répertoire local ou est le fichier a uploader, dossier sur le FTP ou doit etre placé le fichier.
public void Upload(string nomFichier, string repertoireLocal, string dossierFTP)
{
FileInfo fileInf = new FileInfo(repertoireLocal + "/" + nomFichier);//"C:/test.txt"
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.Server + "/" + dossierFTP + "/" + nomFichier));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(this.Username,this.Password);
// By default KeepAlive is true, where the control connection is
// not closed after a command is executed.
reqFTP.KeepAlive = false;
reqFTP.UsePassive = true;
reqFTP.Proxy = null;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[2048];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read
//the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the
// FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch
{
}
}
// parametre : nom du fichier téléchargé sur le FTP, répertoire d'emplacement dans le FTP, emplacement ou sera enregistré le fichier
public void Download(string nomFichier, string repertoireFTP, string repertoireLocal)
{
Uri serverUri = new Uri("ftp://" + this.Server + "/" + repertoireFTP + "/" + nomFichier);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{ return; }
FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri);
myRequest.Credentials = new NetworkCredential(this.Username, this.Password);
myRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)myRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream fs = new FileStream(repertoireLocal + nomFichier, FileMode.Create);
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
fs.Flush();
} while (!(read == 0));
response.Close();
responseStream.Close();
fs.Close();
}
}
} |
Partager