News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

HTTP File Download

Started by thiruvasagamani, May 22, 2009, 12:13 PM

Previous topic - Next topic

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

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

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

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

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

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

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