Labels

slider

Recent

Navigation

Downloader in ASP.net

down loader in asp.net, downloader in c#, file downloader in asp.net, file downloader in C#

Introduction

Today, I am demonstrating a down loader in ASP.net using  with System.Web.WebRequest and System.Web.WebResponse method, if we need to start download on specific length then we can add request range using with Request.Addrange(10000,50000).
Downloader in C#
Down loader in ASP.Net
There is thousands of C# downloader available on internet. We can devise a program to download data from specific web location and put locally on our drive. Here I am providing a program which demonstrates how can we make a program to down loader in asp.net.

Namespace C#

using System.IO;
using System.Web;
using System.Configuration;

Namespace VB.Net

Imports System.IO
Imports System.Web
Imports System.Configuration
Imports System.Exception
We can provide file size here, so all the bytes streams will be downloaded properly.
I have made web request using HTTPWebRequest method:

HTTPWebRequest method:

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(str);

Request Timeout

You can set request timeout as follows:
request.Timeout = 30000000;

Read Buffer Size

int read = strm.Read(inBuf, 0, inBuf.Length);
Here we can provide to read file from 0 bytes to up to your requirement.

Start VB.Net Code Snippet

Private Sub DownloadCode()
Try
       Dim filesize As Integer = 9899267
   Dim fs As FileStream = New FileStream("C:\downloadfolder\1.mp3", FileMode.Create, FileAccess.Write)
   Dim file_start As Integer = 0
   Dim file_end As Integer = filesize
   Dim strm As Stream = Nothing
   Dim offset As Integer = 0

   While file_end >= file_start
   Dim str As String = "http://www.downloaddomain.net/abc/1.mp3"
       'daisy server --- dbt_001
       Dim request As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(str), System.Net.HttpWebRequest)
       'Dim request As System.Net.HttpWebRequest = WebRequest.Create(str)
       request.Timeout = 30000000

       Dim inBuf(4095) As Byte  'for dtb_001

       Dim bytesToRead As Integer = CInt(inBuf.Length)

       Dim bytesRead As Integer = 0

       Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), System.Net.HttpWebResponse)
       'Dim response As HttpWebResponse = request.GetResponse()

       strm = response.GetResponseStream()

       Dim read As Integer = strm.Read(inBuf, 0, inBuf.Length)

       fs.Write(inBuf, 0, read)
       offset += read

       If offset >= file_end Then
           Exit While
       Else
           file_start += 4095
       End If
       request.Abort()
       response.Close()

   End While

   MessageBox.Show("file Downloaded successfully")
        Catch ex As Exception
   MessageBox.Show("file download unsuccessful" & ex.Message)
        End Try
    End Sub

C# Down loader code

Private void DownloadCode()
    {
    try
     {
     int filesize = 9899267;
     FileStream fs = new FileStream("C:\\downloadfolder\\1.mp3", FileMode.Create, FileAccess.Write);
     int file_start = 0;
     int file_end = filesize;
     Stream strm = null;
     int offset = 0;

     while (file_end >= file_start) {
      string str = "http://www.downloaddomain.net/abc/1.mp3";
      //
      System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(str);
      //Dim request As System.Net.HttpWebRequest = WebRequest.Create(str)
      request.Timeout = 30000000;

      byte[] inBuf = new byte[4096];
      //for dtb_001

      int bytesToRead = Convert.ToInt32(inBuf.Length);

      int bytesRead = 0;

      System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
      //Dim response As HttpWebResponse = request.GetResponse()

      strm = response.GetResponseStream();

      int read = strm.Read(inBuf, 0, inBuf.Length);

      fs.Write(inBuf, 0, read);
      offset += read;

      if (offset >= file_end) {
         break; // TODO: might not be correct. Was : Exit While
      }
else
{
         file_start += 4095;
      }
      request.Abort();
      response.Close();

     }

     MessageBox.Show("file Downloaded successfully");
     }
    catch (Exception ex)
    {
     MessageBox.Show("file download unsuccessful" + ex.Message);
    }
}

Relevant Reading:


Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: