/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request">请求</param> /// <param name="streamReader">数据读取器</param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, ISessionStreamReader streamReader, string boundary) { var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = streamReader.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List <HttpFile>(); var form = new HttpNameValueCollection(); streamReader.Position = streamReader.Position + boundaryBytes.Length; while (streamReader.Position < maxPosition) { var headLength = streamReader.IndexOf(DoubleCRLF) + DoubleCRLF.Length; if (headLength < DoubleCRLF.Length) { break; } var head = streamReader.ReadString(Encoding.UTF8, headLength); var bodyLength = streamReader.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.TryGetFileName(out string fileName) == true) { var bytes = streamReader.ReadArray(bodyLength); var file = new HttpFile(mHead.Name, fileName, bytes); files.Add(file); }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request"></param> /// <param name="buffer"></param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, IReceiveBuffer buffer, string boundary) { var doubleCrlf = Encoding.ASCII.GetBytes("\r\n\r\n"); var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = buffer.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List <HttpFile>(); var form = new HttpNameValueCollection(); buffer.Position = buffer.Position + boundaryBytes.Length; while (buffer.Position < maxPosition) { var headLength = buffer.IndexOf(doubleCrlf) + doubleCrlf.Length; if (headLength < doubleCrlf.Length) { break; } var head = buffer.ReadString(headLength, Encoding.UTF8); var bodyLength = buffer.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.IsFile == true) { var stream = buffer.ReadArray(bodyLength); var file = new HttpFile(mHead, stream); files.Add(file); } else { var byes = buffer.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(mHead.Name, value); } buffer.Position = buffer.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request"></param> /// <param name="stream"></param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, IStreamReader stream, string boundary) { var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = stream.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List <HttpFile>(); var form = new HttpNameValueCollection(); stream.Position = stream.Position + boundaryBytes.Length; while (stream.Position < maxPosition) { var headLength = stream.IndexOf(HttpRequestParser.DoubleCrlf) + HttpRequestParser.DoubleCrlf.Length; if (headLength < HttpRequestParser.DoubleCrlf.Length) { break; } var head = stream.ReadString(Encoding.UTF8, headLength); var bodyLength = stream.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.IsFile == true) { var bytes = stream.ReadArray(bodyLength); var file = new HttpFile(mHead, bytes); files.Add(file); } else { var byes = stream.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(mHead.Name, value); } stream.Position = stream.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }
/// <summary> /// http文件 /// </summary> /// <param name="head">头内容</param> /// <param name="stream">数据流</param> internal HttpFile(MultipartHead head, byte[] stream) { this.Name = head.Name; this.FileName = head.FileName; this.Stream = stream; }
/// <summary> /// http文件 /// </summary> /// <param name="head">头内容</param> /// <param name="stream">数据流</param> internal HttpFile(MultipartHead head, byte[] stream) { this.Name = head.Name ; this.FileName = head.FileName ; this.Stream = stream; }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request"></param> /// <param name="buffer"></param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, IReceiveBuffer buffer, string boundary) { var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = buffer.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List<HttpFile>(); var form = new HttpNameValueCollection(); buffer.Position = buffer.Position + boundaryBytes.Length; while (buffer.Position < maxPosition) { var headLength = buffer.IndexOf(Protocol.DoubleCrlf) + Protocol.DoubleCrlf.Length; if (headLength < Protocol.DoubleCrlf.Length) { break; } var head = buffer.ReadString(headLength, Encoding.UTF8); var bodyLength = buffer.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.IsFile == true) { var stream = buffer.ReadArray(bodyLength); var file = new HttpFile(mHead, stream); files.Add(file); } else { var byes = buffer.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(mHead.Name, value); } buffer.Position = buffer.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }