Thursday, October 6, 2011

How to upload/download file to/from server using WebClient method…?

Today’s business applications are working on client server architecture and on timely basis files are being uploaded to server from client or vice versa.

In ASP.Net we can upload a file to server using File-Upload control. But what-if we want to upload a file automatically or programmatically to the server using Windows application, Windows Service or Web application?

The possible ways to upload a file to server are follows:
  • FTP Upload 
  • HTTP Upload
  • ...
Here I will show how to upload/download a file using HTTP method (i.e. commonly known as Web Client method).

In Web Client method we require a virtual directory to which file is to be upload under domain name (i.e. http://www.YourDomainName.com/ClientFiles/). So we are going to upload files to “ClientFiles” directory.

Note: You need to install and configure WebDAV (Web Distributed Authoring and Versioning) in IIS. Click here for an article on Installing and Configuring WebDAV on IIS 7.

Code to upload a file on Server using WebClient method:

System.Net.WebClient webClient = new System.Net.WebClient();
string sourceFilePath = @"D:\MyDocuments\DataFile.xml" ;
string webAddress = "http://www.YourDomainName.com/ClientFiles/";
string destinationFilePath= webAddress + "DataFile.xml";
webClient.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
webClient.UploadFile(destinationFilePath, "PUT", sourceFilePath);
webClient.Dispose();


Code to download a file from Server using WebClient method:

System.Net.WebClient webClient = new System.Net.WebClient();
string webAddress = "http://www.YourDomainName.com/ClientFiles/";
string sourceFilePath = webAddress + "DataFile.xml";
string destinationFilePath = @"D:\MyDocuments\DataFile.xml";
webClient.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
webClient.DownloadFile(sourceFilePath, destinationFilePath);
webClient.Dispose();




Learn by diving in Programming Ocean...
Happy Programming!!!

13 comments:

  1. thanks for your post. I have tried your example in IIS7.5 but get one of these two errors

    1)The remote server returned an error: (404) Not Found.
    2) The remote server returned an error: (405) Method Not Allowed.

    Have you tried this is IIS7? I'm assuming there is some sort of handler setup involved but I can't work it out.

    ReplyDelete
    Replies
    1. just to clarify. it works for downloads but uploading with the PUT method does not.

      Delete
    2. Its working perfectly with IIS 7

      And the uploading file also works with PUT, the first code snippet is related to PUT method only.

      If you have any more queries drop a mail on chiragvidani@gmail.com

      Delete
  2. Works for me too. Thanks a lot!

    ReplyDelete
  3. Hi
    I have a requirement i know the web share for files.But i do not know the names of files.So how can i download multiple files.

    Thanks in advance

    ReplyDelete
  4. Thanks for the quick intro, saved a lot of time!

    ReplyDelete
  5. Will it also work with https?
    In case the above code doesn't works. Kindly advise

    ReplyDelete
    Replies

    1. You need to validate certificate, if you get TLS/SSL error. Below line will help solve error

      ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

      Ref: http://stackoverflow.com/questions/536352/webclient-https-issues

      Delete
    2. What will be the powershell syntax for this?
      ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

      Kindly advise

      Delete
    3. Sorry Prateek, I work on C#.Net, so can't help you on powersheel syntax.

      Delete
    4. No Problem Chirag. Thanks for help :)

      Delete
  6. Hi I have tried this on IIS 7.5 and its working fine.
    But its not working in IIS 8 and IIS 8.5. Do you have any solution for this?

    I am getting the following errors.
    The remote server returned an error: (405) Method Not Allowed. (IIS 8.5)


    ReplyDelete
  7. hi, i understand this is very old thread. but i have a question regarding file size limit here, I am getting "System.Net.WebException: The remote server returned an error: (413) ENTITY TOO LARGE." Is there any work around for this? how to increase the limit?

    ReplyDelete