This worked for a txt/zip file download but if i want to save/download video/audio file.How to move ?

//Two textboxes used here,
  //1.to take URL
  //2.to get saving location on drive

  private void downloadbtn_Click(object sender, EventArgs e)
   {

    WebClient myWebClient = new WebClient();

    string path;
    path =  this.savePath.Text;
    Uri tmp = new Uri(this.downloadURL.Text);
    string EndPathFileName = tmp.Segments.Last();
    path = path + @"\" + EndPathFileName ;
    myWebClient.DownloadFile(this.downloadURL.Text, path );
  }

I tried http://freewavesamples.com/roland-gr1-orchestra-hit-c5.wav which downloaded something but unable to play it

Your problem is most likely caused by the fact that the url you posted leads to a 'Page not found' error page, rather than a wav file!

Comments
Yes,u were rite

I wrote code for a simple downloader but wanna proceed far as my this code only supports zip/pdf downloading. Here is code:

//downloadURL is a textbox to handle user entered URI's
// savePath is for providing disk location to save

private void downloadbtn_Click(object sender, EventArgs e)
{

    WebClient myWebClient = new WebClient();

    string path;
    path =  this.savePath.Text;
    Uri tmp = new Uri(this.downloadURL.Text);
    string EndPathFileName = tmp.Segments.Last();
    path = path + @"\" + EndPathFileName ;
    myWebClient.DownloadFile(this.downloadURL.Text, path );
}

1) It didn't download audio/video How do I do this?

2) It doesn't support uri's starting other than http/https/ftp etc.

example:math.hws.edu/eck/cs124/downloads/javanotes6-linked.pdf
is not handled using this. Why?

Suggest me please

1) What url are you using as a test for this? The last one you posted was broken.

2) If http/https/ftp isn't given, assume http, so do a check for whether the uri starts with http/https/ftp etc., and if not, just add http:// to the front of it. This is the default behaviour of a web browser in that situation.

Ok , i added

downloadURL =  this.downloadURL.Text;
downloadURL = "http://" + downloadURL;
 Uri tmp = new Uri(downloadURL);

and added
www.cs.odu.edu/~mukka/cs795sum14.net/.../C%23tutorialpart1.pdf

but a *.pdf file with no data got!!!
STILL not fruitful

Add using statement: using System.Net;

Pre-requisites:

  • TextBox named: downloadURLTextBox
  • TextBox named: savePathTextBox
  • Button named: downloadBtn

Declare the following variables inside the class:

private string downloadURL = string.Empty;
private string path = string.Empty;

Create a method that gets and checks user input and appends / pre-pends as necessary:

private void getInput()
{
    //get data from textboxes
    downloadURL = downloadURLTextBox.Text;
    path = savePathTextBox.Text;

    //lower case downloadURL
    string lcDownloadURL = downloadURL.ToLower();

    //pre-pend "http://" if user didn't specify
    if (!(lcDownloadURL.StartsWith("http://") ||
          lcDownloadURL.StartsWith("https://") ||
          lcDownloadURL.StartsWith("ftp://")))
    {
        downloadURL = "http://" + downloadURL;
    }//if

    //ensure path ends with "\"
    if (!path.EndsWith(@"\"))
    {
        path = path + @"\";
    }//if

    Uri downloadURI = new Uri(downloadURL);
    string EndPathFileName = downloadURI.Segments.Last();
    path = path + EndPathFileName;
}//getInput

Download the file:

private void downloadFile()
{
    WebClient myWebClient = new WebClient();

    getInput();

    myWebClient.DownloadFile(downloadURL, path);
    myWebClient.Dispose();
}//downloadFile

Double-click the button (downloadBtn) to create "downloadBtn_Click". Then add the following code:

downloadFile();

It should look like:

private void downloadBtn_Click(object sender, EventArgs e)
{
    downloadFile();
}

Edited 2 Years Ago by cgeier

This article has been dead for over six months. Start a new discussion instead.