Implementing file downloads with C # Builder

Source: Internet
Author: User
Tags file size file upload resource thread
Download

A Overview:
This article introduces some basic knowledge of Internet communication programming with C # Builder through an example. We know. NET class includes the request/response layer, the Application protocol layer, the Transport layer and so on. In this program, we use the WebRequest class at the request/response level and the WebClient class to achieve a high degree of abstraction in Internet communication services. The function of this program is to complete the download of the file.
Two Implementation principle:
The principle of program implementation is relatively simple, mainly used WebClient class and FileStream class. Where the WebClient class is in the System.Net namespace, the primary function of the class is to provide a public method for sending data to the resource identified by the URI and receiving data from the resource identified by the URI. We use the Downfile () method to download the files locally. The file data is then written to the local file using the instance object of the FileStream class as a data stream. This completes the download of the file.
Three Implementation steps:
1. First, open C # builder,file->new->c# Application,name here we set to "download".
2. Setting of the main boundary. Text set to "File Download", StartPosition set to Centerscreen,maximizebox set to False, we add the following controls on the main form: two Label control Label1,label2, a text box control TextBox1, A button control button1 and a progress bar control progressBar1.
Label1:text as URL; Label2:text for download progress; Textbox1:text set to null; Button1:text set to download;
3. Coding of the program
Process Downfile, used to complete the download of the file
private void Downfile ()
{
String FileName;
WebClient downfile=new WebClient ();
Long fbytes;
if (textbox1.text!= "")
{
Savefiledialog1.showdialog ();
Filename=savefiledialog1.filename;
if (filename!= "")
{
Get File size
WebRequest wr_request=webrequest.create (TextBox1.Text);
WebResponse wr_response=wr_request. GetResponse ();
Fbytes=wr_response. ContentLength;
progressbar1.maximum= (int) fbytes;
Progressbar1.step=1;
Wr_response. Close ();
Start downloading data
Downfile.downloaddata (TextBox1.Text);
Stream strm = Downfile.openread (TextBox1.Text);
StreamReader reader = new StreamReader (STRM);
byte[] MByte = new Byte[fbytes];
int allmybyte = (int) MByte. Length;
int startmbyte = 0;
while (fbytes>0)
{
int m = strm. Read (Mbyte,startmbyte,allmybyte);
if (m==0) break;
Startmbyte+=m;
Allmybyte-=m;
Progressbar1.value+=m;
}
FileStream FSTRM = new FileStream (filename,filemode.openorcreate,fileaccess.write);
Fstrm. Write (Mbyte,0,startmbyte);
Strm. Close ();
Fstrm. Close ();
Progressbar1.value=progressbar1.maximum;
}
} else
{
MessageBox.Show ("Did not enter the file to download!") ");
}
}

Double-click the Download button to enter the following code:
Thread th = new Thread (new ThreadStart (Downfile));
Th. Start ();

The complete code is as follows:
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Net;
Using System.IO;
Using System.Threading;

namespace download
{
<summary>
Summary description for WinForm.
</summary>
public class WinForm:System.Windows.Forms.Form
{
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.TextBox TextBox1;
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.SaveFileDialog SaveFileDialog1;
Private System.Windows.Forms.Label Label2;
Private System.Windows.Forms.ProgressBar progressBar1;

Public WinForm ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();

//
Todo:add any constructor the code after InitializeComponent call
//
}

<summary>
Clean up any being used.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code
<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This.label1 = new System.Windows.Forms.Label ();
This.textbox1 = new System.Windows.Forms.TextBox ();
This.button1 = new System.Windows.Forms.Button ();
This.savefiledialog1 = new System.Windows.Forms.SaveFileDialog ();
This.label2 = new System.Windows.Forms.Label ();
THIS.PROGRESSBAR1 = new System.Windows.Forms.ProgressBar ();
This. SuspendLayout ();
//
Label1
//
This.label1.Location = new System.Drawing.Point (40, 40);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (40, 16);
This.label1.TabIndex = 0;
This.label1.Text = "URL:";
//
TextBox1
//
This.textBox1.Location = new System.Drawing.Point (72, 36);
This.textBox1.Name = "TextBox1";
This.textBox1.Size = new System.Drawing.Size (256, 21);
This.textBox1.TabIndex = 1;
This.textBox1.Text = "";
//
Button1
//
This.button1.Location = new System.Drawing.Point (256, 120);
This.button1.Name = "Button1";
This.button1.TabIndex = 2;
This.button1.Text = "Download";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Label2
//
This.label2.Location = new System.Drawing.Point (8, 80);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (72, 23);
This.label2.TabIndex = 3;
This.label2.Text = "Download progress:";
//
ProgressBar1
//
This.progressBar1.Location = new System.Drawing.Point (72, 80);
This.progressBar1.Name = "ProgressBar1";
This.progressBar1.Size = new System.Drawing.Size (256, 16);
This.progressBar1.TabIndex = 4;
//
WinForm
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (360, 173);
This. Controls.Add (THIS.PROGRESSBAR1);
This. Controls.Add (THIS.LABEL2);
This. Controls.Add (This.button1);
This. Controls.Add (This.textbox1);
This. Controls.Add (THIS.LABEL1);
This. MaximizeBox = false;
This. Name = "WinForm";
This. StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
This. Text = "File Download";
This. ResumeLayout (FALSE);
}
#endregion

<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New WinForm ());
}

private void Downfile ()
{
String FileName;
WebClient downfile=new WebClient ();
Long fbytes;
if (textbox1.text!= "")
{
Savefiledialog1.showdialog ();
Filename=savefiledialog1.filename;
if (filename!= "")
{
Get File size
WebRequest wr_request=webrequest.create (TextBox1.Text);
WebResponse wr_response=wr_request. GetResponse ();
Fbytes=wr_response. ContentLength;
progressbar1.maximum= (int) fbytes;
Progressbar1.step=1;
Wr_response. Close ();
Start downloading data
Downfile.downloaddata (TextBox1.Text);
Stream strm = Downfile.openread (TextBox1.Text);
StreamReader reader = new StreamReader (STRM);
byte[] MByte = new Byte[fbytes];
int allmybyte = (int) MByte. Length;
int startmbyte = 0;
while (fbytes>0)
{
int m = strm. Read (Mbyte,startmbyte,allmybyte);
if (m==0) break;
Startmbyte+=m;
Allmybyte-=m;
Progressbar1.value+=m;
}
FileStream FSTRM = new FileStream (filename,filemode.openorcreate,fileaccess.write);
Fstrm. Write (Mbyte,0,startmbyte);
Strm. Close ();
Fstrm. Close ();
Progressbar1.value=progressbar1.maximum;
}
} else
{
MessageBox.Show ("Did not enter the file to download!") ");
}
}
private void Button1_Click (object sender, System.EventArgs e)
{
Thread th = new Thread (new ThreadStart (Downfile));
Th. Start ();
}
}
}

4. Press F9 to run the program, enter the address, click the "Download" button to try.


Four Conclusion:
The above is a simple example to show you how to use C # Builder implementation of the file download, we can not find it difficult to use C # Builder Internet communication programming is very convenient. In the above program, we only use the WebClient class of some methods, and WebClient class not only provides the method of downloading files, but also provides a file upload methods, interested readers may wish to try their own, check help.
Multi-Threading is used in the program because the WebClient class occupies a large amount of resources, and downloading the file will make the display of the entire window incomplete. If you don't use multiple threads, you won't be able to move windows or do other things when downloading files.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.