无法转换类型为#System; Net.FileWebRequest'的对象输入' System.Net.HttpWebRequest'

时间:2015-01-22 08:24:23

标签: c# .net c#-4.0 c#-3.0

我在尝试上传到FTP时遇到上述错误。但是,当我尝试从本地计算机运行此代码时,它会给出错误。好心提醒。

这里我的代码如下:

 static void Main(string[] args)
    {

        var yourListOfFilePaths = Directory.GetFiles(filepath);

        using (ZipFile zip = new ZipFile())
        {
            foreach (string filePath in yourListOfFilePaths)
            {
                zip.AddFile(filePath);    // FILE PATH LOCATION / WHICH FOLDER FILES YOU WANTED TO ZIP
                zip.Password = "abc1234"; // CHANGE YOUR PASSWORD HERE 
            }
            zip.Save(ZipPath + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("http://www.bitrix24.com/" + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("jayden@bitrix24.com", "abc123");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader(ZipPath + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");
            byte[] fileContents = File.ReadAllBytes("filepath");
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            request.KeepAlive = false;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close();

        }
    }

1 个答案:

答案 0 :(得分:3)

此:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("http://www.bitrix24.com/"
                              + "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");

你的问题。您正在发送一个以" http"开头的地址而不是" ftp。

更改您的网址:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.bitrix24.com/" + 
                                "\\Batch_" + DateTime.Now.ToString("ddMMyy") + ".zip");