Use the WebClient class to upload files (transfer)

xiaoxiao2021-03-06  80

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.Text; using System.IO; namespace UploadFile {public class Form1: System.Windows .Forms.Form {private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label3; private System.Windows. Forms.Button button2; private System.Windows.Forms.TextBox txtFileName; private System.Windows.Forms.TextBox txtServerPath; private System.Windows.Forms.LinkLabel linkLabel1; private System.ComponentModel.Container components = null; public Form1 () { InitializeComponent ();} protected override void Dispose (bool disposing) {if (disposing) {if (components = null!) {components.Dispose ();}} base.Dispose (disposing);} # region Windows Form Designer generated codeprivate Void InitializationComponent () {system.resources.resourceManager Resources = New System.Resources.ResourceManager (Typeo f (Form1)); this.label1 = new system.windows.forms.label (); this.txtServerPath = new system.windows.Forms.TextBox (); this.label2 = new system.windows.forms.label () idton1 = new system.windows.Forms.Button (); this.txtFileName = new system.windows.Forms.TextBox (); this.label3 = new system.windows.forms.Label (); this.button2 = New system.windows.Forms.Button (); this.linklabel1 = new system.windows.forms.linklabel (); this.suspendlayout (); // // label1 // this.label1.forecolor = system.drawing.color .Red; this.label1.location = new system.drawing.point (8, 96); this.label1.name = "label1"; this.label1.size = new system.drawing.size (448, 16); this .label1.tabindex = 0;

this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // txtServerPath // this.txtServerPath.Location = new System.Drawing.Point (128, 8); this.txtServerPath.Name = "txtServerPath"; this .txtServerPath.Size = new system.drawing.size (320, 21); this.txtServerPath.Tabindex = 1; this.txtServerPath.Text = "http: // mengxianhui / aspxweb / images /" ;; /// Label2 // this.label2.autosize = true; this.label2.font = new system.drawing.font ("Song", 9F, System.drawing.FontStyle.bold, System.drawing.graphicsUnit.Point (((System.byte (134)))); this.label2.forecolor = system.drawing.color.navy; this.label2.location = new system.drawing.point (8, 12); this.label2.name = "label2"; this .label2.size = new system.drawing.size (116, 17); this.label2.tabindex = 2; this.Label2.text = "Please enter the server address:"; this.label2.textalign = system.drawing.contentalignment .MiddleCenter; // // Button1 // this.button1.location = new system.drawing.point (192, 64); this.button1.name = "button1"; this.button1.tabindex = 3; this.button1. TEXT = "upload file "; This.button1.Click = new System.EventHandler (this.button1_Click); this.button1.MouseDown = new System.Windows.Forms.MouseEventHandler (this.button1_MouseDown); // // txtFileName // this.txtFileName .Location = new system.drawing.point (128, 32); this.txtFileName.Name = "txtFileName"; this.txtFileName.size = new system.drawing.size (232, 21); this.txtFileName.taBIndex = 4 "this.txtFilename.text =" "; // // label3 // this.label3.autosize = true; this.label3.font = new system.drawing.font (" Song "

, 9F, System.drawing.FontStyle.Bold, System.drawing.graphicsUnit.Point, ((System.byte))); this.label3.forecolor = system.drawing.color.darkBlue; this.label3.location = New system.drawing.point (8, 38); this.label3.name = "label3"; this.label3.size = new system.drawing.size (116, 17); this.label3.tabindex = 5; this .label3.text = "Please enter the upload file name:"; this.label3.textalign = system.drawing.contentalignment.middleCenter; // // button2 // this.button2.location = new system.drawing.point (370, 32); this.button2.name = "button2"; this.button2.size = new system.drawing.size (80, 23); this.button2.tabindex = 6; this.button2.text = "Browse file ..." This.Button2.click = new system.eventhandler (this.button2_click); // // linklabel1 // this.linklabel1.location = new system.drawing.point (16, 120); this.linklabel1.name = " linkLabel1 "; this.linkLabel1.Size = new System.Drawing.Size (440, 24); this.linkLabel1.TabIndex = 7; this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.linkLabel1.LinkClicked = NEW system. Windows.Forms.LinkLabelLinkClickedEventHandler (this.linkLabel1_LinkClicked); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size (6, 14); this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = New system.drawing.size (464, 157); this.controls.add (this.LinkLabel1); this.controls.add (this.button2); this.controls.add (this.txtFileName); this.controls.add (this.TXTSERVERPATH); this.controls.add (this.Label2); this.Controls.add (this.button1); this.controls.add (this.label1). ;. THIS. This.icon = ((System.drawing.icon) ("$ this.icon")))

This.Name = "form1"; this.text = "WEBCLIENT Upload file example"; this.resize = new system.eventhandler (this.form1_resize); this.ResumeLayout (false);} # endregion [stathread] static void Main () {Application.run (New Form1 ());} ///

///. NET SDK Type /// private filestream OpenFile () {OpenFiledialog Dlgopenfile = New OpenFileDialog (); dlgOpenFile.ShowReadOnly = true; if (dlgOpenFile.ShowDialog () == DialogResult.OK) {if (dlgOpenFile.ReadOnlyChecked == true) {return (FileStream) dlgOpenFile.OpenFile ();} else {string path = dlgOpenFile.FileName; return new FileStream (path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);}} return null;} private void button2_Click (object sender, System.EventArgs e) {OpenFileDialog dlgOpenFile = new OpenFileDialog (); dlgOpenFile.InitialDirectory = @ "C: /"; dlgOpenFile.ShowReadOnly = false; dlgOpenFile.ReadOnlyChecked = true; dlgOpenFile.Filter = "All files (* *.) | * *."; if (dlgOpenFile.ShowDialog () == DialogResult.ok) {if (dlgopenfile.readonlyChecked == true) {txtFileName.text = DLGOPENFILE.FILENAME.TOSTRING ();}}} private void button1_click (object sender, system.eventargs e) {// Need to note: TXTSERVERPATH folder has an anonymous permissions.

/ / You can define new file names if (txtFilename.Text.trim () == "" || txtserverpath.text.trim () == "") {messagebox.show ("Please enter the name of the file you want to upload! "," Error: ", MessageBoxButtons.ok, MessageBoxicon.information;} else {/// Get file name, file extension name, server path string filenamepath = txtFileName.Text.trim (); string uristring = txtserverPath.Text. Trim (); string fileName = fileNamePath.Substring (fileNamePath.LastIndexOf ( "//") 1); string fileNameExt = fileName.Substring (fileName.LastIndexOf () 1 "."); if (uriString.EndsWith ( " / ") == false) uriString = uriString " / "; uriString = uriString fileName; /// create WebClient instance WebClient myWebClient = new WebClient (); myWebClient.Credentials = CredentialCache.DefaultCredentials; // to upload files FileStream FS = New filestream (filename, filemode.open, fileaccess.read); // filestream fs = OpenFile (); binaryreader r = new binaryReader (FS); try {// Using the UPLoadFile method You can use the following format // mywebclient. UploadFile (uristring, "put", filenamepath; byte [] posarray = r.readbytes ((int) fs.length); stream poststream = MyWebClient.OpenWrite (Uristring, "PUT"); if (POS) TStream.canwrite) {PostStream.write (postarray, 0, postarray.Length); label1.text = filename "upload success! "} else {label1.text =" file is not written now! "} poststream.close (); linklabel1.text =" View uploaded file "; for (int i = linklabel1.links.count - 1; i> -1; i -) linkLabel1.Links.Remove (LinkLabel1. Links [i]); linkLabel1.Links.Add (0, LinkLabel1.Text.length, uristring);} catch (webException errmsg) {label1.text = "upload failed:" errmsg.Message;

转载请注明原文地址:https://www.9cbs.com/read-107441.html

New Post(0)