Archive

Archive for November, 2012

Upload file from Windows application to asp.net web server

November 17, 2012 Leave a comment

Its simple with the WebClient class. Just include this code inside the Windows client application.

  System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add(“Content-Type”, “binary/octet-stream”);
byte[] result = Client.UploadFile(“http://localhost:57361/Default.aspx”, “POST”, file);
String s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);

MessageBox.Show(s);

The Default.aspx page as receiver look like this

  protected void Page_Load(object sender, EventArgs e)
{

string fname = Request.Files[0].FileName;

Request.Files[0].SaveAs(Server.MapPath(Path.Combine(“~/Images/”, fname)));

Response.Write(“Uploaded”);
Response.End();

}

You can download the sample from here

https://skydrive.live.com/redir?resid=6DAACA1E5AB7FFE1!886&authkey=!AAIgGN9RfSK72Kg

happy coding 🙂