下面的代碼可以post任何類型檔案,主要是把資料流統一轉換為base64格式.//------------------------------發送部分-------------------------------------------------- string url = "http://localhost/im/upfile?aa=5";//發送到的頁面的地址 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); //讀取一個圖片 FileStream fs = new FileStream(Server.MapPath("~/NET基礎知識.doc"), System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] filecontent = new byte[fs.Length]; fs.Read(filecontent, 0, filecontent.Length); fs.Close(); fs.Dispose(); //將圖片轉換成base64編碼的流 string a = Convert.ToBase64String(filecontent); //讀取base64編碼流,發送 byte[] requestBytes = System.Text.Encoding.Default.GetBytes(a); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); //接收返回參數,到string backstr HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default); string backstr = sr.ReadToEnd(); sr.Close(); res.Close(); //輸出參數 Response.Write(backstr);//-----------------------------------------------接收部分----------------------------------- //接收到的參數 string bb= Request.QueryString["aa"]; Encoding myEncoding = Encoding.GetEncoding("utf-8"); //接收傳遞過來的資料流 Stream resStream = Request.InputStream; byte[] filecontent = new byte[resStream.Length]; //將資料流讀入byte數組 resStream.Read(filecontent, 0, filecontent.Length); //數群組轉換為string以便轉換base64使用 string a = myEncoding.GetString(filecontent); //將string讀取base64解密到byte數組 byte[] filecontent2 = Convert.FromBase64String(a); //寫入目錄 File.WriteAllBytes(Server.MapPath("~/"+bb+".doc"), filecontent2); //傳回值 Response.Write("ok"); Response.End();