欢迎光临
我们一直在努力

利用WebClient类向服务器上载文件-ASP教程,ASP应用

建站超值云服务器,限时71元/月

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 code

private void initializecomponent()

{

system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(form1));

this.label1 = new system.windows.forms.label();

this.txtserverpath = new system.windows.forms.textbox();

this.label2 = new system.windows.forms.label();

this.button1 = 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("宋体", 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 = "请输入服务器地址:";

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 = "上载文件";

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("宋体", 9f, system.drawing.fontstyle.bold,

system.drawing.graphicsunit.point, ((system.byte)(134)));

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 = "请输入上传文件名:";

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 = "浏览文件…";

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.label3);

this.controls.add(this.txtserverpath);

this.controls.add(this.label2);

this.controls.add(this.button1);

this.controls.add(this.label1);

this.icon = ((system.drawing.icon)(resources.getobject("$this.icon")));

this.name = "form1";

this.text = "webclient 上传文件的例子";

this.resize += new system.eventhandler(this.form1_resize);

this.resumelayout(false);

}

#endregion

[stathread]

static void main()

{

application.run(new form1());

}

/// <summary>

/// .net sdk 上面的打开文件的类

/// </summary>

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 = "所有文件 (*.*)|*.*";

if(dlgopenfile.showdialog() == dialogresult.ok)

{

if(dlgopenfile.readonlychecked == true)

{

txtfilename.text = dlgopenfile.filename.tostring();

}

}

}

private void button1_click(object sender, system.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(filename, 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, system.windows.forms.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, system.eventargs e)

{

if(this.windowstate == formwindowstate.maximized) this.windowstate = formwindowstate.normal;

}

private void button1_mousedown(object sender, system.windows.forms.mouseeventargs e)

{

if(txtfilename.text.trim() != "" && txtserverpath.text.trim() != "")

label1.text = "正在上传文件,请稍侯……!";

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用WebClient类向服务器上载文件-ASP教程,ASP应用
分享到: 更多 (0)