
Introduction
This article describes about a common requirement of transferring files from one web server to another web server.
Background
When I was working in some project, there was a requirement where I had a web application (http://someserver/someapplication) from where I had to transfer the files to another web server (http://www.someotherserver/someotherapplication). So, I thought why should not I share the same with you all.
Using the code
Download the WebRequest project and WebRequestReceiver project. Install it in a convenient location, e.g.: c:\inetpub\wwwroot. The ZIP file contains both projects.
This article has two projects:
- A main project which contains the code to upload/download files.
- To transfer/upload a file from one webserver to another webserver (frmUploadFile.aspx).
- To retrieve/download a file from the other webserver to the location machine (frmDownloadFile.aspx).
- A Web Request Receiver project which will save the files that are posted to it.
The application makes use of the WebClient
class provided in the .NET framework.
try
{
WebClient oclient = new WebClient();
byte[] responseArray =
oclient.UploadFile(txtURLToSend.Text,"POST",txtFileToSend.Text);
lblStatus.Text =
"Check the file at " + Encoding.ASCII.GetString(responseArray);
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
try
{
WebClient oclient = new WebClient();
oclient.DownloadFile(txtURL.Text,txtFileLocation.Text);
lblStatus.Text = "Check the file at " + txtFileLocation.Text;
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
When I say uploading the file from one web server to another web server, it basically posts the file from one web server to another web server like the way a file from the client side is posted when you use <input type="file">
.
Points of Interest
This example is useful in places where you have to post files from one web server to another web server.