HTTP File Download

Started by thiruvasagamani, May 22, 2009, 05:43 PM

previous topic - next topic
Go Down

thiruvasagamani

HTTP File Download

Once the upload portion of the application is built, adding download functionality is easy. We'll start by adding a DownloadFile method and accept the local file name and download URL as parameters.
Copy Code

Code: [Select]
public void DownloadFile(string localFile, string downloadUrl)

The HttpWebRequest object is created the same as before, but this time we'll set the Method to "GET" to retrieve a file from the server.
Copy Code

Code: [Select]
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
req.Method = "GET";


Unlike "PUT", "GET" specifies all necessary information in the URL. It therefore does not need to send any data with the request to enable write stream buffering - the request can simply be submitted by calling GetResponse. This time, we need to keep the GetResponse return value.
Copy Code

Code: [Select]
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();

The returned HttpWebResponse represents all information returned from the server including the status code, headers, and any returned data. The returned data (which in the case of a "GET" is the contents of the server's file specified by the URL) is accessed through the HttpWebResponse response stream.
Copy Code

Code: [Select]
Stream respStream = resp.GetResponseStream();

If line-by-line access to the file is required, the stream can be read using the ReadLine method of StreamReader with each line being written to the local file using a StreamWriter.
Copy Code

Code: [Select]
public void DownloadFile(string localFile, string downloadUrl)
{
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
  req.Method = "GET";

  HttpWebResponse resp = (HttpWebResponse) req.GetResponse();

  // Retrieve response stream and wrap in StreamReader
  Stream respStream = resp.GetResponseStream();
  StreamReader rdr = new StreamReader(respStream);

  // Create the local file
  StreamWriter wrtr = new StreamWriter(localFile);

  // loop through response stream reading each line
  //  and writing to the local file
  string inLine = rdr.ReadLine();
  while (inLine != null)
  {
    wrtr.WriteLine(inLine);
    inLine = rdr.ReadLine();
  }

  rdr.Close();
  wrtr.Close();
}


Just as in the case of the upload, reading the stream directly and using a FileStream to create the local file provides a more efficient download that also supports non-text files such as MP3s, bitmaps, and .NET assemblies.
Copy Code

Code: [Select]
public void DownloadFileBinary(string localFile, string downloadUrl)
{
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
  req.Method = "GET";

  HttpWebResponse resp = (HttpWebResponse) req.GetResponse();

  // Retrieve response stream
  Stream respStream = resp.GetResponseStream();

  // Create local file
  FileStream wrtr = new FileStream(localFile, FileMode.Create);

  // Allocate byte buffer to hold stream contents
  byte[] inData = new byte[4096];

  // loop through response stream reading each data block
  //  and writing to the local file
  int bytesRead = respStream.Read(inData, 0, inData.Length);
  while (bytesRead > 0)
  {
    wrtr.Write(inData, 0, bytesRead);
    bytesRead = respStream.Read(inData, 0, inData.Length);
  }

  respStream.Close();
  wrtr.Close();
}


Source : MSDN
Thiruvasakamani Karnan


dhoni

this program is more useful to download thehttp file easy way

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens | GinGly :: SMS Backup | Acumen :: Discussion Board | AshokPillar :: Hosting | CineBuzz :: Latest Cinema News | My Kids Diary :: Capture your kids magical moment
Copyright 2005 - 2017 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 10 years - Powered by HostGator Dedicated Server