Tuesday, August 31, 2010

How to copy a file from one server to another server.

Using FTP:
If you have valid FTP username and password to the target server we can use web client and UploadFile(...) method.
NetworkCredential networkCredentials = new NetworkCredential("UserName""Password");
WebClient client = new WebClient();
client.Credentials = networkCredentials;
Byte[] resposne = client.UploadFile("ftp://www.my-website.com/Image.jpg""C:\\Data\\Image.jpg");

Using HTTP:
If you dont have a valid FTP account, you can hand over the file to target server but from the target server should accept the file save it by itself (security consideration, make sence). Possibly using a generic http handler.
From the Source Server:
Byte[] response = 
    new WebClient().UploadFile("http://www.my-website.com/FileHandler.ashx""C:\\StockMarketUpdate.xls");

From the Target Server:
public class FileHandler : IHttpHandler
    public void ProcessRequest(HttpContext context)
        foreach (string f in context.Request.Files.AllKeys)
            HttpPostedFile file = context.Request.Files[f];
            string fileName = Path.Combine(context.Server.MapPath("~/Data"), file.FileName);
            if (File.Exists(fileName)) File.Delete(fileName);
    public bool IsReusable
            return false;

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...