ASP.NET 中将文件上传到另外一个服务器的方法二将文件上传到网络共享服务器的方法...

前面的文章,我提供了一种” 将文件上传到网络共享服务器的方法“,这次,我们采用FTP的方法将文件上传到另外一台服务器上。
首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了。代码如下:
ASPX 代码
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @ Page Language = " C# " EnableViewState = " false " %>

<% @ Import Namespace = " System.Net " %>
<% @ Import Namespace = " System.IO " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< script runat ="server" >
protected
void Button1_Click(object sender, EventArgs e)
{
// 要接收文件的 ftp 服务器地址
String serverUri = " ftp://192.168.3.1/ " ;
String fileName
= Path.GetFileName(FileUpload1.FileName);
serverUri
+= fileName;

FtpWebRequest request
= (FtpWebRequest)WebRequest.Create(serverUri);
request.Method
= WebRequestMethods.Ftp.AppendFile;
request.UseBinary
= true ;
request.UsePassive
= true ;

// ftp 服务器上允许上传的用户名和密码
request.Credentials = new NetworkCredential( " upload " , " upload " );
Stream requestStream
= request.GetRequestStream();
Byte[] buffer
= FileUpload1.FileBytes;

requestStream.Write(buffer,
0 , buffer.Length);
requestStream.Close();
FtpWebResponse response
= (FtpWebResponse)request.GetResponse();
Label1.Text
= response.StatusDescription;
response.Close();
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 孟宪会之将文件上传到另外一个服务器的方法二 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:FileUpload ID ="FileUpload1" runat ="server" />
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="上传文件" />
< div >< asp:Label ID ="Label1" runat ="server" Text ="" ></ asp:Label ></ div >
</ form >
</ body >
</ html >

ASPX 代码
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @ Page Language = " C# " EnableViewState = " false " %>

<% @ Import Namespace = " System.Net " %>
<% @ Import Namespace = " System.IO " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< script runat ="server" >
protected
void Button1_Click(object sender, EventArgs e)
{
// 要接收文件的 ftp 服务器地址
String serverUri = " ftp://192.168.3.1/ " ;
String fileName
= Path.GetFileName(FileUpload1.FileName);
serverUri
+= fileName;

FtpWebRequest request
= (FtpWebRequest)WebRequest.Create(serverUri);
request.Method
= WebRequestMethods.Ftp.AppendFile;
request.UseBinary
= true ;
request.UsePassive
= true ;

// ftp 服务器上允许上传的用户名和密码
request.Credentials = new NetworkCredential( " upload " , " upload " );
Stream requestStream
= request.GetRequestStream();
Byte[] buffer
= FileUpload1.FileBytes;

requestStream.Write(buffer,
0 , buffer.Length);
requestStream.Close();
FtpWebResponse response
= (FtpWebResponse)request.GetResponse();
Label1.Text
= response.StatusDescription;
response.Close();
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 孟宪会之将文件上传到另外一个服务器的方法二 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:FileUpload ID ="FileUpload1" runat ="server" />
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="上传文件" />
< div >< asp:Label ID ="Label1" runat ="server" Text ="" ></ asp:Label ></ div >
</ form >
</ body >
</ html >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值