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:
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:
Code to download a file from Server using WebClient method:
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
- ...
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();
|
|
thanks for your post. I have tried your example in IIS7.5 but get one of these two errors
ReplyDelete1)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.
just to clarify. it works for downloads but uploading with the PUT method does not.
DeleteIts working perfectly with IIS 7
DeleteAnd 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
Works for me too. Thanks a lot!
ReplyDeleteHi
ReplyDeleteI 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
Thanks for the quick intro, saved a lot of time!
ReplyDeleteWill it also work with https?
ReplyDeleteIn case the above code doesn't works. Kindly advise
DeleteYou 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
What will be the powershell syntax for this?
DeleteServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Kindly advise
Sorry Prateek, I work on C#.Net, so can't help you on powersheel syntax.
DeleteNo Problem Chirag. Thanks for help :)
DeleteHi I have tried this on IIS 7.5 and its working fine.
ReplyDeleteBut 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)
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