急求:C# 基于WIN窗体的:如何将本地文件上传到服务器 ,望给出源代码,谢谢! 
      如将本地磁盘d盘的文件 2008.doc 上传到服务器192.168.10.2上的文件“上传文件”中,如何实现 
      望高手指点!

解决方案 »

  1.   

    用IIS建了一个FTP FTP可以通过地址访问,但怎样通过代码来访问FTP呢
    谢谢 能帮我写个么 
      

  2.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OracleClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO; 
    namespace test
    {
    /// <summary>
    /// postexercise 的摘要说明。
    /// </summary>
    public class postexercise : System.Web.UI.Page
    {
     protected System.Web.UI.WebControls.Button btnsubmit1;
      protected System.Web.UI.WebControls.Label lblMsg1;
      protected System.Web.UI.WebControls.Label lblMsg2;
      protected System.Web.UI.WebControls.Button btnsubmit;
      protected System.Web.UI.HtmlControls.HtmlInputFile file1;
      protected System.Web.UI.HtmlControls.HtmlInputFile File2;
      private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此处放置用户代码以初始化页面
      }
    private void btnsubmit_Click(object sender, System.EventArgs e)
      {
       int res=0;
       if(res==-999)
       {
        lblMsg1.Text="文件上传失败1 !";
        return;
       }
       //获得文件名
       string filename=System.IO.Path.GetFileName(file1.PostedFile.FileName);
       filename=Server.MapPath("../test/Excel/")+filename;
       
       //保存上传文件
       res=SaveExcelFile(filename);
       if(res==1)
       {
        lblMsg1.Text="文件上传成功 !";
       }
       else
       {
        lblMsg1.Text="文件上传失败2 !";
        return;
       }
      }   
         private int SaveExcelFile(string filename)
           {
            try
               {
             file1.PostedFile.SaveAs(filename);
             return 1;
               }
           catch
              {
            return -999;
               }
           }}
    }
      

  3.   


    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");
    }
    }以上是采用.Net所带的FtpWebRequest类实现的,
    楼主也可以用完全自己完成FTp协议的方法实现,可以参考以下链接:
    http://www.csharphelp.com/archives/archive9.html
      

  4.   


    private void Upload(string filename) 中的filename 应该是本地文件的目录吧》?
    string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 中的fileInf.Name 就是只本地文件filename文件的名字么?想问你的是:本地文件的目录到底付给哪个参数?