62,046
社区成员
发帖
与我相关
我的任务
分享
internal static byte[] GetBytesFromStream(Stream stream, System.Text.Encoding encoding)
{
// Read the stream into a byte array
byte[] data = ToByteArray(stream);
List<byte> dataList = new List<byte>();
// Copy to a string for header parsing
string content = encoding.GetString(data);
string matchValue = "Content-Transfer-Encoding: binary\r\n\r\n";
int lastIndex = content.LastIndexOf(matchValue);
int startingIndex = lastIndex + matchValue.Length;
byte[] bytes = encoding.GetBytes(content.Substring(0, startingIndex - 1));
int bytesLength = bytes.Length;
for (int i = bytesLength + 1; i < data.Length; i++)
{
dataList.Add(data[i]);
}
return dataList.ToArray();
}
还有一种是使用MultipartParser
参考:
Reading file input from a multipart/form-data POST
public void Upload(Stream stream)
{
MultipartParser parser = new MultipartParser(stream);
if (parser.Success)
{
// Save the file
SaveFile(parser.Filename, parser.ContentType, parser.FileContents);
}
}
使用 WCF 实现 RESTful 方式的 WebService