从ftp服务器下载文件?

时间:2014-03-01 13:33:32

标签: c# asp.net ftp ftp-client

protected void LinkButton1_Click(object sender, EventArgs e)
 {
    string filename = @"C:\inetpub\ftproot\RetailAgreement\abc.JPG"; 
    string ftpServerIP = "192.148.10.10";
    string ftpUserID = "username";
    string ftpPassword = "password";

    System.IO.FileInfo fileInf = new FileInfo(filename);

    string uri = "ftp://" + ftpServerIP + "/RetailAgreement/" + fileInf.Name;
    FtpWebRequest reqFTP;      
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/RetailAgreement/" + fileInf.Name));
    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    int buffLength = 2048;
    byte[] buff = new byte[buffLength];
    int contentLen;
    FileStream fs = fileInf.OpenRead();                   

    Stream strm = reqFTP.GetRequestStream();

        // Read from the file stream 2kb at a time
        contentLen = fs.Read(buff, 0, buffLength);

        // Till Stream content ends
        while (contentLen != 0)
        {

            // Write Content from the file stream to the FTP Upload Stream
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
        }
        // Close the file stream and the Request Stream
        strm.Close();
        fs.Close();             
}

这里我得到了例外 FileStream fs = fileInf.OpenRead(); 找不到文件'C:\ inetpub \ ftproot \'.. 但我的路径是存在正确的路径,我给了..我们改变了代码,请帮助我?

0 个答案:

没有答案