Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I have one site which give me one file. When i open URL in Browser that time i get one file.

I have use below code
C#
webBrowser1.Navigate(uri);


But this will open save dialog. I do not use save dialog box. I want to directly download file when navigate webBrowser.

Please Help me

What I have tried:

I try webBrowser1.Navigate(uri) method for download but it will open Save As Dialog.
Posted
Updated 1-Sep-16 22:17pm

C#
Method:-1 Download File Synchronously
The following code shows how to download file synchronously. This method blocks the main thread until the file is downloaded or an error occur (in this case the WebException is thrown).

C#
using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");


Method:-2 Download File Asynchronously
C#
To download file without blocking the main thread use asynchronous method DownloadFileA­sync. You can also set event handlers to show progress and to detect that the file is downloaded.


C#
private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
  progressBar.Value = e.ProgressPercentage;
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}
 
Share this answer
 
Comments
Savalia Manoj M 2-Sep-16 4:50am    
Thanks for Help me... I already check this but did not get success.
Can you add this link "https://www.nseindia.com/content/historical/DERIVATIVES/2016/AUG/fo05AUG2016bhav.csv.zip"... this will return zip file
You can't do this for a number of reasons. The main and obvious one being security...would you want a website saving files on your hard drive?
 
Share this answer
 
Comments
Savalia Manoj M 2-Sep-16 4:56am    
Yes, i want to save website file into my harddrive
F-ES Sitecore 2-Sep-16 5:06am    
Well you can't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900