如何通过UpLoad File控件ASP.NET将文件从本地计算机上传到其他计算机?

时间:2013-03-11 08:41:45

标签: c# asp.net code-behind

我的本​​地机器上有一个文件。我想使用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();
        }

0 个答案:

没有答案
相关问题