我现在做一个B/S的程序,想做一个上传功能,点击button1按钮后,将一个动态生成的文件保存成文本test.tex后再上传至另外一台电脑,我的程序放在服务器比如是192.168.2.6,而我现在要上传至192.168.2.5,而我现在是在客户端操作,如何办。高手麻烦提示一下。(其实文本文件里面是动态生成的html代码)

解决方案 »

  1.   

    webserivece或ftp(http当然也可以,不过很弱)。
      

  2.   

    呵呵,我刚刚做完一个上传功能
    客户端代码
      string filePath = txtFilePath.Text.Trim();
                try
                {
                    if (filePath != null && txtBacPath.Text.Trim() != null)
                    {
                        if (Directory.Exists(filePath))
                        {
                            //获取该文件夹下所有文件路径
                            string[] files = Directory.GetFiles(filePath);
                            foreach (string file in files)
                            {
                                WebClient client = new WebClient();
                                //上载文件
                                client.UploadFile("http://192.168.1.200:7000/download/DownLoad/UploadFile.aspx", "POST", file);
                            }
                            
                        }
                    }
                }
                catch (Exception error)
                {
                    
                } 服务端代码
     try
                    {
                        //判断文件夹是否存在
                        if (!Directory.Exists(HttpRuntime.AppDomainAppPath + @"DownLoad\Files\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + DateTime.Now.Day.ToString() + "\\"))
                        {
                            //文件夹不存在,创建文件夹
                            Directory.CreateDirectory(HttpRuntime.AppDomainAppPath + @"DownLoad\Files\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + DateTime.Now.Day.ToString() + "\\");
                        }
                        foreach (string file in Request.Files.AllKeys)
                        {
                            //获取文件
                            HttpPostedFile hpFile = Request.Files[file];
                            //文件路径
                            string filePath = HttpRuntime.AppDomainAppPath + @"DownLoad\Files\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + DateTime.Now.Day.ToString() + "\\" + hpFile.FileName;
                            //保存文件
                            hpFile.SaveAs(filePath);                    }
                    }
                    catch (Exception error)
                    {
                        //异常日志
                        
                    }    
                }
    客户端代码相当于,你要实现上传功能的页面,服务端代码就是你发布的一个页面也就是
    http://192.168.1.200:7000/download/DownLoad/UploadFile.aspx
      

  3.   

    没有成功呀。hyx199012
    能不能再解释一下。
      

  4.   

    直接用Fileupload控件,最简单的方法,save成服务器的路径即可。服务器路径sever.mappat
      

  5.   

    应该能成功的啊,你有没有将你接受上传文件的页面发布到iis上?
      

  6.   

     <form runat="server">
        <input id="File1" type="file"  runat="server" />
        <asp:Button ID="Button2" runat="server"  onclick="Button2_Click1" 
         Text="Button" />
     </form> Button2_Click1事件这么处理
       protected void Button2_Click1(object sender, EventArgs e)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                string UserPath = Server.MapPath("Image").ToString();
                //如果文件夹不存在则创建
                if (!System.IO.Directory.Exists(UserPath))
                {
                    System.IO.Directory.CreateDirectory(UserPath);
                }            string fileName2 = System.IO.Path.GetFileName(Request.Files[i].FileName);
                //Response.Write(UserPath + "\\" + fileName2);            Request.Files[i].SaveAs(UserPath + "\\" + fileName2);        }  
        }
      

  7.   

    原来如此,那你为何这样来呢,为了管理方便!!那这样也好做,在我上面的代码中继续加入上传。上传到192.168.2.5中,前提是192.168.2.5中也有个ASP.NET web接收文件。
      

  8.   

    服务器代码就是指你要在哪里接受上传文件啊。不需要触发事件的只要有上传就可以接受,不过要将代码放到load事件厘米
      

  9.   

    在192.168.2.6中放入这个方法,在刚才的for循环 Request.Files[i].SaveAs(UserPath + "\\" + fileName2);
    处加上这个方法 if( Upload_Client(http://192.168.2.5/default.aspx,UserPath + "\\" + fileName2) )
                  File.Delete(UserPath + "\\" + fileName2);
     /// <summary> 
            /// 将本地文件上传到指定的服务器(WebClient方法) 
            /// </summary> 
            /// <param name="address">文件上传到的服务器</param> 
            /// <param name="fileNamePath">要上传的本地文件(全路径)</param> 
            /// <returns>成功返回1,失败返回0</returns> 
            public static int Upload_Client(string address, string fileNamePath)
            {
                WebClient wc = new WebClient();
                FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
                address = string.Concat(address, "?filename=", Path.GetFileName(fileNamePath));
                Stream poststream = wc.OpenWrite(address, "POST");
                byte[] buffer = new byte[1024];
                int mum = (int)fs.Length;
                //已上传的字节数 
                long offset = 0;
                int length = fs.Read(buffer, 0, buffer.Length);//读取长度
                //开始上传时间 
                DateTime startTime = DateTime.Now;
                while (length > 0)
                {
                    poststream.Write(buffer, 0, length);
                    offset += length;
                    TimeSpan span = DateTime.Now - startTime;
                    double second = span.TotalSeconds;
                    Console.WriteLine("已用时:" + second.ToString("F2") + "秒");
                    if (second > 0.1)
                    {
                        Console.WriteLine(" 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒");
                    }
                    else
                    {
                        Console.WriteLine(" 正在连接…");
                    }
                    Console.WriteLine("已上传:" + (offset * 100.0 / mum).ToString("F2") + "%");
                    Console.WriteLine((offset / 1048576.0).ToString("F2") + "M/" + (mum / 1048576.0).ToString("F2") + "M");
                    Application.DoEvents();
                    length = fs.Read(buffer, 0, buffer.Length);
                }
                poststream.Close();
                if (wc.QueryString.ToString() == "Success")
                    return 1;
                else
                    return 0;
            }然后 在192.168.2.5中,Default.aspx文件
      
      protected void Page_Load(object sender, EventArgs e)
        {ReceiveFile();}  private void ReceiveFile()
        {
            System.IO.Stream sr = Request.InputStream;
            if (sr.Length > 0)
            {
                string filename = Request.QueryString["filename"];
                int size = 1024;
                byte[] by = new byte[size];
                int length = sr.Read(by, 0, size);
                System.IO.FileStream fs = new System.IO.FileStream(Path.Combine(Server.MapPath("Image"), filename), System.IO.FileMode.Create, System.IO.FileAccess.Write);
                while (length > 0)
                {
                    fs.Write(by, 0, by.Length);
                    length = sr.Read(by, 0, size);
                }
                fs.Close();
                sr.Close();
                Response.Write("Success\r\n");
            }        if (Request.Files.Count > 0)
            {
                try
                {                HttpPostedFile file = Request.Files[0];
                    string filePath = Server.MapPath("\\Image\\" + file.FileName);
                    file.SaveAs(filePath);
                    Response.Write("Success\r\n");
                }
                catch
                {
                    Response.Write("Error\r\n");
                }
            }    }
      

  10.   

    好了,非常感谢csdn和朋友们,谢谢。