你的位置:首页 > ASP.net教程

[ASP.net教程]winform 里 如何实现文件上传


看了网上写的 用webclient类的uploadfile方法,

我在本地建立了个webform,winform窗体,  现在可以本地实现文件传递,可以选择文件传到d:\temp路径下,但怎们传到服务器上就不会了

求教大神

webform code

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class SiteMaster : System.Web.UI.MasterPage 9 {10   protected void Page_Load(object sender, EventArgs e)11   {12     foreach (string f in Request.Files.AllKeys)13     {14       HttpPostedFile file = Request.Files[f];15       file.SaveAs(@"D:\Temp\" +file.FileName);16     }17     if (Request.Params["testKey"] != null)18     {19       Response.Write(Request.Params["testKey"]);20     } 21   }22 }

winform code:

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO;10 using System.Net;11 using System.Web;12 13 namespace 上传14 {15   public partial class UpLoadForm : Form16   {17     public UpLoadForm()18     {19       InitializeComponent();20     }21     private void btn_select_Click(object sender, EventArgs e)22     {23       OpenFileDialog ofd = new OpenFileDialog();24       ofd.Multiselect = true;25       if (ofd.ShowDialog() == DialogResult.OK)26       {27         this.txtpath.Text = ofd.FileName;28       }29     }30     private void btn_Upload_Click(object sender, EventArgs e)31     {32       if (this.txtpath.EditValue != null)33       {34        UpLoadFile("http://localhost:61750/WebSite3/Default.aspx", "POST", this.txtpath.Text.Trim());35        // UpLoadFile("\\192.168.5.71", "POST", this.txtpath.Text.Trim());36       }37       else38       {39         MessageBox.Show("请选择上传文件");40       }41      42     }43     public void UpLoadFile(string Serverpath, string Mode, string Localpath)44     {45       try46       {47         WebClient mywebcilent = new WebClient();        48         mywebcilent.UploadFile(Serverpath,Mode,Localpath);49         MessageBox.Show("上传成功");50       }51       catch52       {53         MessageBox.Show("上传失败");54       } 55     }56 57   58   }59 }