how to make a web page where user can download file

Posted by Shitalr under ASP.NET on 9/6/2013 | Points: 10 | Views : 1006 | Status : [Member] | Replies : 4
hello,
i want code for download file using webclient method please help me.
and for upload also.




Responses

Posted by: Allemahesh on: 9/6/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Shital,

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();


Link :

http://chiragvidani.blogspot.in/2011/10/how-to-uploaddownload-file-tofrom.html
http://msdn.microsoft.com/en-us/library/ez801hhe.aspx

Happy Coding

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Shitalr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Downloading Files
There are two ways of downloading a file from a web site using WebClient, depending on whether we want to save the file, or process the contents of the directly within your application. If we simply want to save the file then we should call the DownloadFile() method takes two parameters, The URL from where we want to retrieve the file, and the file name ( or path) that we want to save the file to.

The solution is very simple and below is the code which achieves the goal. Here I am downloading a file asynchronously on Button Click
private void buttonDownloadFile_Click(object sender, EventArgs e)

{
string url = @"http://chandu.files.wordpress.com/2013/09/chandu.jpg";

//Create an instance of WebClient
WebClient client = new WebClient();

//Hookup DownloadFileCompleted Event
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

//Start the download and copy the file to D:\Pics
client.DownloadFileAsync(new Uri(url), @"D:\PIcs\chandu.jpg");
}

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("File downloaded");
}


Reference: http://www.csharp-examples.net/download-files/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shitalr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
//Sample code to download files using WebClient
string remoteUri = "http://www.contoso.com/library/homepage/images/";

string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);


References: http://msdn.microsoft.com/en-us/library/ez801hhe.aspx
http://www.csharp-examples.net/download-files/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shitalr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Shitalr,
Have you solved the above issue...
If yes Mark the replies which helped you...

This too http://www.dotnetfunda.com/forums/thread16194-application-which-can-download-a-file-and-upload-file.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shitalr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response