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.
Example:
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.
Example:
From the Source Server:
Byte[] response = 
    new WebClient().UploadFile("http://www.my-website.com/FileHandler.ashx""C:\\StockMarketUpdate.xls");

From the Target Server:
FileHanlder.ashx
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);
            file.SaveAs(fileName);
        }
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...