private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            UploadFile(openFileDialog1.FileName, "d:\\1", false);
        }
        public static bool UploadFile(string localFilePath, string serverFolder, bool reName)
        {
            string fileNameExt, newFileName, uriString;
            if (reName)
            {
                fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
                newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
            }
            else
            {
                newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
            }
            if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("\\"))
            {
                serverFolder = serverFolder + "/";
            }            uriString = serverFolder + newFileName;   //服务器保存路径
            /**/
            /// 创建WebClient实例
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;            // 要上传的文件
            FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
            // FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
            try
            {
                //使用UploadFile方法可以用下面的格式
                //myWebClient.UploadFile(uriString,"PUT",localFilePath);
                byte[] postArray = r.ReadBytes((int)fs.Length);
                //    Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
                Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                }
                else
                {
                    return false;
                }
                postStream.Close();
            }
            catch
            {
                //    //MessageBox.Show("文件上传失败,请稍候重试~");
                return false;
            }            return true;
        }
这样能传到本地d盘文件夹1下面 
如何上传到远程服务器上呢 
把"d:\\1"换成ftp://123.456.789.20/wtt为什么就不行呢? 
当然这个ftp是个假的  

解决方案 »

  1.   

    myWebClient.Credentials = CredentialCache.DefaultCredentials;
    一般FTP服务器都需要密码的,这样做不行。
      

  2.   

    WebClient.UploadFile 方法
    public byte[] UploadFile (
    string address,
    string fileName
    )
    参数
    address
    接收文件的资源的 URI。例如,ftp://localhost/samplefile.txt。fileName
    要发送到资源的文件。例如,“samplefile.txt”。UploadFile 方法将本地文件发送到资源。此方法使用 STOR 命令上载 FTP 资源。对于 HTTP 资源,使用 POST 方法。此方法在上载文件时阻止。若要在等待服务器的响应的同时继续执行,请使用 UploadFileAsync 方法之一。POST 方法由 HTTP 定义。如果基础请求不使用 HTTP 并且 POST 不为服务器所理解,则所发生的情况将由基础协议类决定。通常将引发 WebException,同时设置 Status 属性指示错误。如果 BaseAddress 属性不是空字符串 (""),且 address 不包含绝对 URI,则 address 必须是相对 URI,此 URI 与 BaseAddress 组合在一起构成所请求数据的绝对 URI。如果 QueryString 属性不是空字符串,则将它追加到 address。
      

  3.   

    貌似远程传服务器  必须要用到SOKECT 这个东西
    不知道这是什么东西
      

  4.   

    下面的代码示例使用 UploadFile 将指定的文件上载到指定的 URI。由服务器返回的任何响应都显示到控制台。Console.Write("\nPlease enter the URI to post data to : ");
    String uriString = Console.ReadLine();// Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    myWebClient.Credentials =new NetworkCredential(username,password);//对应ftp服务器上的用户名和密码Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
    string fileName = Console.ReadLine();
    Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);// Upload the file to the URI.
    // The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
    byte[] responseArray = myWebClient.UploadFile(uriString,fileName);// Decode and display the response.
    Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}", 
        System.Text.Encoding.ASCII.GetString(responseArray));
      

  5.   

    ftp和本地复制是有本质区别的,协议都不同...如果你是局域网且有权限的话可以用 \\XXX.XXX.XXX.XXX\AAA\ 这样的路径,不要用ftp,就跟本地复制一样了. 如果你是用域管理局域网则更方面
      

  6.   


    private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                UploadFile(openFileDialog1.FileName, "ftp://222.128.30.142/WTTSOFT/wtt", false);
            }
            public  bool UploadFile(string localFilePath, string serverFolder, bool reName)
            {
                string fileNameExt, newFileName, uriString;
                string username = "wttsoft";
                string password = "wttsoft";
                if (reName)
                {
                    fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
                    newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
                }
                else
                {
                    newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
                }
                if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("\\"))
                {
                    serverFolder = serverFolder + "/";
                }            uriString = serverFolder ;   //服务器保存路径
                /**/
                /// 创建WebClient实例
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = new NetworkCredential(username, password);//对应ftp服务器上的用户名和密码            // 要上传的文件
                FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                try
                {
                    //使用UploadFile方法可以用下面的格式
                    myWebClient.UploadFile(uriString, "PUT", localFilePath);
                    byte[] postArray = r.ReadBytes((int)fs.Length);
                    Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
                    //Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
                    //byte[] responseArray = myWebClient.UploadFile(uriString, newFileName);
                    //byte[] responseArra = myWebClient.UploadFile(uriString, newFileName);
                    if (postStream.CanWrite)
                    {
                        postStream.Write(postArray, 0, postArray.Length);
                    }
                    else
                    {
                        return false;
                    }
                    postStream.Close();
                }
                catch
                {
                    //    //MessageBox.Show("文件上传失败,请稍候重试~");
                    return false;
                }            return true;
            }错误是请求的Url对于此ftp命令无效
      

  7.   

    private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                UploadFile(openFileDialog1.FileName, "ftp://222.126.31.131/wtt", false);
            }
    public  bool UploadFile(string localFilePath, string serverFolder, bool reName)
            {
                string fileNameExt, newFileName, uriString;
                string username = "WTTSOFT";
                string password = "WTTSOFT";
                if (reName)
                {
                    fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
                    newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
                }
                else
                {
                    newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
                }
                if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("\\"))
                {
                    serverFolder = serverFolder + "/";
                }            uriString = serverFolder+newFileName ;   //服务器保存路径
                /**/
                /// 创建WebClient实例
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = new NetworkCredential(username, password);//对应ftp服务器上的用户名和密码            // 要上传的文件
                FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                try
                {
                   byte[] postArray = myWebClient.UploadFile(uriString, localFilePath);
                }
                catch
                {
                    //    //MessageBox.Show("文件上传失败,请稍候重试~");
                    return false;
                }            return true;
            }
    现在程序无任何错误  就是服务器里没有
    没有上传成功
    为什么 大侠帮忙啊
      

  8.   

    string username="*****"; 
                string password="********T"; 
                string uri = "ftp://******/wtt/UnicodeDemo.cs"; 
                string fileName = "C:\\UnicodeDemo.cs"; 
                WebClient client = new WebClient(); 
                client.Credentials = new NetworkCredential(username, password); 
                byte[] result=null; 
                try 
                { 
                    result = client.UploadFile(uri, fileName); 
                } 
                catch (WebException exc) 
                { 
                    MessageBox.Show(exc.Status.ToString()); 
                } 
                string resultString = System.Text.Encoding.ASCII.GetString(result); 
                MessageBox.Show(resultString); 是可以的。
      

  9.   


    private void Upload(string filename) 
      { 
       FileInfo fileInf = new FileInfo(filename); 
       string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 
       FtpWebRequest reqFTP; 
       
       // 根据uri创建FtpWebRequest对象 
       reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); 
       
       // ftp用户名和密码 
       reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
       
       // 默认为true,连接不会被关闭 
       // 在一个命令之后被执行 
       reqFTP.KeepAlive = false; 
       
       // 指定执行什么命令 
       reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
       
       // 指定数据传输类型 
       reqFTP.UseBinary = true; 
       
       // 上传文件时通知服务器文件的大小 
       reqFTP.ContentLength = fileInf.Length; 
       
       // 缓冲大小设置为2kb 
       int buffLength = 2048; 
       
       byte[] buff = new byte[buffLength]; 
       int contentLen; 
       
       // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 
       FileStream fs = fileInf.OpenRead(); 
       try 
       { 
       // 把上传的文件写入流 
       Stream strm = reqFTP.GetRequestStream(); 
       
       // 每次读文件流的2kb 
       contentLen = fs.Read(buff, 0, buffLength); 
       
       // 流内容没有结束 
       while (contentLen != 0) 
       { 
       // 把内容从file stream 写入 upload stream 
       strm.Write(buff, 0, contentLen); 
       
       contentLen = fs.Read(buff, 0, buffLength); 
       } 
       
       // 关闭两个流 
       strm.Close(); 
       fs.Close(); 
       } 
       catch (Exception ex) 
       { 
       MessageBox.Show(ex.Message, "Upload Error"); 
       } 
      } 看看有没有帮助。