我在本地机器上有一个文件。我想使用 ASP.NET 的上传文件控件上传到其他机器。这台机器是在 VMWare 软件上运行的 CentOS。
HTML
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<asp:FileUpload ID="FileUpload1" runat="server" ToolTip="Select Only Excel File" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>
代码隐藏
public void ftpfile(string ftpfilepath, string inputfilepath)
{
ftpfilepath = "/jetnexus";
string ftphost = "192.168.2.19";
//here correct hostname or IP of the ftp server to be given
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential("root", "jetnexus");
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}