前面的文章,我提供了一种”
将文件上传到网络共享服务器的方法“,这次,我们采用FTP的方法将文件上传到另外一台服务器上。
首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在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 >
<% @ 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 >
<% @ 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 >