FTP Webservice

This is an FTP Webservice that allows you perform FTP operations over HTTP. It requires the FTP component from FTPClient.co.uk

Here is the source code:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Web;

using System.Web.Services;

using System.IO;

using FTP;

namespace FTPWebservice

{

/// <summary>

/// Perform FTP operations over HTTP

/// </summary>

[WebService(Namespace = "http://ftpclient.co.uk/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

public class FTPWebservice : System.Web.Services.WebService

{

public static FtpClient ftpClient = new FtpClient();

[WebMethod]

public string Connect(string server, string username, string password)

{

ftpClient.register("mcisel1999");

return ftpClient.connect(server, username, password);

}

[WebMethod]

public void disconnect()

{

ftpClient.disconnect();

}

[WebMethod]

public List<RemoteFileOrFolder> getRemoteFolder(string path)

{

List<RemoteFileOrFolder> lTree = new List<RemoteFileOrFolder>();

ArrayList alItems = ftpClient.getRemoteFolder(path);

foreach(RemoteFileOrFolder ftpItem in alItems)

{

lTree.Add(ftpItem);

}

return lTree;

}

[WebMethod]

public void upload(byte[] fileData,string filename, string remotePath)

{

if (filename.IndexOf("\\")!=-1 || filename.IndexOf("/")!=-1)

{

throw new Exception("Filename invalid");

}

FileStream fs = new FileStream(filename,FileMode.CreateNew);

fs.Write(fileData,0,fileData.Length);

fs.Flush();

fs.Close();

ftpClient.upload(filename, remotePath);

File.Delete(filename);

}

[WebMethod]

public string download(string remoteFile)

{

ftpClient.download(remoteFile,"temp.dat");

FileStream fs = new FileStream("temp.dat",FileMode.Open);

StreamReader sr = new StreamReader(fs);

string strData = sr.ReadToEnd();

sr.Close();

fs.Close();

File.Delete("temp.dat");

return strData;

}

[WebMethod]

public void rename(string remoteFile,string newName)

{

ftpClient.rename(remoteFile,newName);

}

[WebMethod]

public void delete(string remoteFile)

{

ftpClient.delete(remoteFile);

}

[WebMethod]

public void createFolder(string parentFolder,string folderName)

{

ftpClient.createFolder(parentFolder,folderName);

}

}

}