Telecharger des fichiers, par le protocole http

Soyez le premier à donner votre avis sur cette source.

Vue 19 731 fois - Téléchargée 940 fois

Description

bonjour, voila un pt outil pour telecharger des fichiers a partir d'un serveur..

Source / Exemple :


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;

namespace Download
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label urlL;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox url;
		private System.Windows.Forms.SaveFileDialog saveFile;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox fileLocation;
		private System.Windows.Forms.Button download;
		private System.Windows.Forms.TextBox FileName;
		private System.Windows.Forms.Button btnExit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources 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 method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.download = new System.Windows.Forms.Button();
			this.urlL = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.url = new System.Windows.Forms.TextBox();
			this.saveFile = new System.Windows.Forms.SaveFileDialog();
			this.FileName = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.fileLocation = new System.Windows.Forms.TextBox();
			this.btnExit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// download
			// 
			this.download.Enabled = false;
			this.download.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.download.Location = new System.Drawing.Point(144, 112);
			this.download.Name = "download";
			this.download.Size = new System.Drawing.Size(152, 23);
			this.download.TabIndex = 0;
			this.download.Text = "Download File !!";
			this.download.Click += new System.EventHandler(this.download_Click);
			// 
			// urlL
			// 
			this.urlL.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.urlL.Location = new System.Drawing.Point(8, 10);
			this.urlL.Name = "urlL";
			this.urlL.Size = new System.Drawing.Size(40, 16);
			this.urlL.TabIndex = 1;
			this.urlL.Text = "URL :";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "File Name :";
			// 
			// url
			// 
			this.url.Location = new System.Drawing.Point(72, 8);
			this.url.Name = "url";
			this.url.Size = new System.Drawing.Size(328, 20);
			this.url.TabIndex = 3;
			this.url.Text = "http://";
			// 
			// saveFile
			// 
			this.saveFile.FileName = "crack_xp";
			this.saveFile.Filter = "All Files | *.*";
			// 
			// FileName
			// 
			this.FileName.Location = new System.Drawing.Point(72, 46);
			this.FileName.Name = "FileName";
			this.FileName.Size = new System.Drawing.Size(184, 20);
			this.FileName.TabIndex = 4;
			this.FileName.Text = "(The Name Of the file to download it!)";
			// 
			// button2
			// 
			this.button2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(328, 80);
			this.button2.Name = "button2";
			this.button2.TabIndex = 5;
			this.button2.Text = "Browse";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 83);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 6;
			this.label1.Text = "Save As :";
			// 
			// fileLocation
			// 
			this.fileLocation.Location = new System.Drawing.Point(72, 81);
			this.fileLocation.Name = "fileLocation";
			this.fileLocation.Size = new System.Drawing.Size(248, 20);
			this.fileLocation.TabIndex = 8;
			this.fileLocation.Text = "";
			// 
			// btnExit
			// 
			this.btnExit.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnExit.Location = new System.Drawing.Point(328, 112);
			this.btnExit.Name = "btnExit";
			this.btnExit.TabIndex = 9;
			this.btnExit.Text = "EXIT";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(410, 144);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.fileLocation);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.FileName);
			this.Controls.Add(this.url);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.urlL);
			this.Controls.Add(this.download);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Form1";
			this.Text = "Download\'s Tool (By Crack_XP)";
			this.ResumeLayout(false);

		}
		#endregion

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

		private void download_Click(object sender, System.EventArgs e) {
			string remoteUri = url.Text;
			string fileName = FileName.Text, myStringWebResource = null;
			// Create a new WebClient instance.
			WebClient myWebClient = new WebClient();
			// Concatenate the domain with the Web resource filename.
			myStringWebResource = remoteUri + fileName;
			MessageBox.Show("Downloading File \"{0}\" from \"{1}\" .......\n\n"+fileName+myStringWebResource);
			// Download the Web resource and save it into the current filesystem folder.
			myWebClient.DownloadFile(myStringWebResource,fileLocation.Text);        
			MessageBox.Show("Successfully Downloaded File!!");
			MessageBox.Show("Downloaded file saved in the following file system folder:\n\t" + fileLocation.Text);			

		}

		private void button2_Click(object sender, System.EventArgs e) {
			if (saveFile.ShowDialog()==DialogResult.OK){
				fileLocation.Text=saveFile.FileName;
				download.Enabled=true;
				}
				
				
		}

		private void btnExit_Click(object sender, System.EventArgs e) {
			Application.Exit();
		}
	}
}

Conclusion :


voir le zip, et bien respecter le format des (url/repertoir_From/),
nom des fichiers(file.extention)et en fin le l'emlacement et le nom.extention

VOIR LA CAPTURE POUR MIEUX COMPRENDRE

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

sparky_rv
Messages postés
9
Date d'inscription
samedi 17 janvier 2004
Statut
Membre
Dernière intervention
6 octobre 2004
-
salut !! sympa mais bloquant je l ai exuté dans un thread c est nikel parcontre je veux rajouter un barragrph ... comment recuperer la taille du fichier distant ?

pour le thread ca donne ca :
private void downloadURL(String url)
{
try
{
int lastIndex = url.LastIndexOf("/")+1;
string filename = url.Substring(lastIndex);
string pathnfile= Application.StartupPath+"\"+filename;
richTextBox1.AppendText("Downloading File :\n" + url + "\n");
myDownloadFile = new FileInfo(Application.StartupPath+"\"+filename);
myTimer.Enabled = true;
myURL = url;
myFile = pathnfile;
ThreadStart threadStart = new ThreadStart(downloadThread);
thread = new Thread(threadStart);
thread.Start();
richTextBox1.AppendText("\nfile saved in :\n\t" + Application.StartupPath +"\n");

}
catch(Exception e)
{
richTextBox1.AppendText("file NOT downloaded ... erreur during process\n"+e+"\n");
}

}
private void downloadThread()
{
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(myURL,myFile);
myWebClient.Dispose();
}


(on remarquera la presence d un timer c est pour avoir l'evolution du fichier telechargé)

public void OnTimer(object sender, EventArgs e)
{
if(thread.IsAlive)
{
myDownloadFile.Refresh();
richTextBox1.AppendText(myDownloadFile.Length+"\n");
}
else myTimer.Enabled = false;


voila il me manque plus que la taille du fichier distant le rajouter dans mon baragraphe et c est la fet :p
cerig
Messages postés
2
Date d'inscription
mardi 17 décembre 2002
Statut
Membre
Dernière intervention
21 septembre 2005
-

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.