主要思想:把不通形式的文件或者文字,以字節編碼流的形式傳遞過去而後反解析後從新生成原文件數組
//------------------------------發送部分--------------------------------------------------
string url = "http://localhost/im/upfile?aa=5";//發送到的頁面的地址
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//讀取一個圖片
FileStream fs = new FileStream(Server.MapPath("~/123123.jpg"),
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+".jpg"), filecontent2);
//返回值
Response.Write("ok");
Response.End();app