您的位置:首页 > 其它

Winform如何通过TextBox上传图片到服务器

2009-06-15 15:11 459 查看
#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.button2 = new System.Windows.Forms.Button();
this.txtFileName = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtServerPath = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(84, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(113, 45);
this.button1.TabIndex = 0;
this.button1.Text = "监听";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(263, 28);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(292, 21);
this.textBox1.TabIndex = 1;
//
// linkLabel1
//
this.linkLabel1.Location = new System.Drawing.Point(144, 266);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(440, 24);
this.linkLabel1.TabIndex = 15;
this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// button2
//
this.button2.Location = new System.Drawing.Point(498, 178);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 23);
this.button2.TabIndex = 14;
this.button2.Text = "浏览文件…";
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(256, 178);
this.txtFileName.Name = "txtFileName";
this.txtFileName.Size = new System.Drawing.Size(232, 21);
this.txtFileName.TabIndex = 12;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.ForeColor = System.Drawing.Color.DarkBlue;
this.label3.Location = new System.Drawing.Point(136, 184);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(122, 12);
this.label3.TabIndex = 13;
this.label3.Text = "请输入上传文件名:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtServerPath
//
this.txtServerPath.Location = new System.Drawing.Point(256, 154);
this.txtServerPath.Name = "txtServerPath";
this.txtServerPath.Size = new System.Drawing.Size(320, 21);
this.txtServerPath.TabIndex = 9;
this.txtServerPath.Text = "http://www.x.com/WeUpload/";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ForeColor = System.Drawing.Color.Navy;
this.label2.Location = new System.Drawing.Point(136, 158);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(122, 12);
this.label2.TabIndex = 10;
this.label2.Text = "请输入服务器地址:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button3
//
this.button3.Location = new System.Drawing.Point(320, 210);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 11;
this.button3.Text = "上载文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
this.button3.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button3_MouseDown);
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(136, 242);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(448, 16);
this.label1.TabIndex = 8;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 35000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(387, 74);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 16;
this.pictureBox1.TabStop = false;
//
// button4
//
this.button4.Location = new System.Drawing.Point(319, 266);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 17;
this.button4.Text = "打水印";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(662, 317);
this.Controls.Add(this.button4);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.button2);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtServerPath);
this.Controls.Add(this.label2);
this.Controls.Add(this.button3);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox txtFileName;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtServerPath;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button4;

后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.Net;
using System.IO;

using System.Runtime.InteropServices;
using System.Net.Sockets;
using Microsoft.Win32;

#region 上传图片到服务器
private void button3_Click(object sender, EventArgs e)
{
// 需要注意的是:txtServerPath文件夹有匿名可写的权限。
// 可以自己定义新文件名字
if (txtFileName.Text.Trim() == "" || txtServerPath.Text.Trim() == "")
{
MessageBox.Show("请输入你要上载的文件名字!", "错误:", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
/// 得到文件名,文件扩展名字,服务器路径
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;
/// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;

// 要上传的文件
FileStream fs = new FileStream(this.txtFileName.Text, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
label1.Text = fileName + "上传成功!";
}
else
{
label1.Text = "文件目前不可写!";
}
postStream.Close();
linkLabel1.Text = "查看上载的文件";
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 = "上传失败:" + errMsg.Message;
}
}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
string target = e.Link.LinkData as string;
if (null != target)
{
System.Diagnostics.Process.Start(target);
}
else
{
MessageBox.Show("请用浏览器访问:" + target);
}
}

private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal;
}

private void button3_MouseDown(object sender, MouseEventArgs e)
{
if (txtFileName.Text.Trim() != "" && txtServerPath.Text.Trim() != "")
label1.Text = "正在上传文件,请稍侯……!";
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: