無奈之下,想到了一個下下策,那就是通過分析接受到的輸入流,從中提取想要的數據。比如我在上文中提到的數據(兩個參數,一個圖片文件)
實現思路:讀取輸入流,通過非文件部份的分析,確定文件在整個流中的位置和大小,再對輸入流重新讀取以得到文件。
看上面的POST數據,參數部分及分隔符等,說白了都是些字符串?;旧隙际怯⑽臄底址柕?,如有中文發送之前可做一下編碼,這樣基本上可以確保不會因編碼問題造成計算失誤(英文字符各編碼都是相同的)。這一部分內容(非文件部分,文件部分是二進制格式來的,千萬別這樣去做)可以得到。
通過接受輸入的流轉成字符串,如果參數中有中文,請注意客戶端POST時使用何種編碼:
復制代碼 代碼如下:
byte[] input = Request.BinaryRead(Request.TotalBytes);
string source = Encoding.UTF8.GetString(input);
復制代碼 代碼如下:
Regex rginput = new Regex("略");
if (rginput.IsMatch(source))
{
int headLength=Encoding.UTF8.GetBytes(rginput.Match(source).Value)
}
復制代碼 代碼如下:
//保存文件
FileStream fss = new FileStream("path", FileMode.Create);
fss.Write(input, headLength, input.Length-headLength-footLength);
fss.Close();
復制代碼 代碼如下:
//得到文件字節數組
byte[] imgcont = new byte[input.Length - headLength-footLength];
MemoryStream ms = new MemoryStream(input);
//光標移動到文件開始處
ms.Seek(headLength,SeekOrigin.Begin);
ms.Read(imgcont, 0, imgcont.Length);
ms.Close();
新聞熱點
疑難解答
圖片精選