/// <summary> /// Initializes a new instance of the <see cref="MultipartFormDataParser" /> class /// with the boundary, stream, input encoding and buffer size. /// </summary> /// <param name="stream"> /// The stream containing the multipart data /// </param> /// <param name="boundary"> /// The multipart/form-data boundary. This should be the value /// returned by the request header. /// </param> /// <param name="encoding"> /// The encoding of the multipart data /// </param> /// <param name="binaryBufferSize"> /// The size of the buffer to use for parsing the multipart form data. This must be larger /// then (size of boundary + 4 + # bytes in newline). /// </param> public MultipartFormDataParser(Stream stream, string boundary, Encoding encoding, int binaryBufferSize) { Files = new List <FilePart>(); Parameters = new List <ParameterPart>(); var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding, binaryBufferSize); streamingParser.ParameterHandler += parameterPart => Parameters.Add(parameterPart); streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes) => { if (Files.Count == 0 || name != Files[Files.Count - 1].Name) { Files.Add(new FilePart(name, fileName, new MemoryStream(), type, disposition)); } Files[Files.Count - 1].Data.Write(buffer, 0, bytes); }; streamingParser.Run(); // Reset all the written memory streams so they can be read. foreach (var file in Files) { file.Data.Position = 0; } }
/// <summary> /// Parse the stream with the boundary, input encoding and buffer size. /// </summary> /// <param name="stream"> /// The stream containing the multipart data. /// </param> /// <param name="boundary"> /// The multipart/form-data boundary. This should be the value /// returned by the request header. /// </param> /// <param name="encoding"> /// The encoding of the multipart data. /// </param> /// <param name="binaryBufferSize"> /// The size of the buffer to use for parsing the multipart form data. This must be larger /// then (size of boundary + 4 + # bytes in newline). /// </param> private void ParseStream(Stream stream, string boundary, Encoding encoding, int binaryBufferSize) { Files = new List <FilePart>(); Parameters = new List <ParameterPart>(); var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding, binaryBufferSize); streamingParser.ParameterHandler += parameterPart => Parameters.Add(parameterPart); streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber) => { if (partNumber == 0) { // create file with first partNo Files.Add(new FilePart(name, fileName, Utilities.MemoryStreamManager.GetStream(), type, disposition)); } Files[Files.Count - 1].Data.Write(buffer, 0, bytes); }; streamingParser.Run(); // Reset all the written memory streams so they can be read. foreach (var file in Files) { file.Data.Position = 0; } }
/// <summary> /// Initializes a new instance of the <see cref="MultipartFormDataParser" /> class /// with the boundary, stream, input encoding and buffer size. /// </summary> /// <param name="stream"> /// The stream containing the multipart data /// </param> /// <param name="boundary"> /// The multipart/form-data boundary. This should be the value /// returned by the request header. /// </param> /// <param name="encoding"> /// The encoding of the multipart data /// </param> /// <param name="binaryBufferSize"> /// The size of the buffer to use for parsing the multipart form data. This must be larger /// then (size of boundary + 4 + # bytes in newline). /// </param> public MultipartFormDataParser(Stream stream, string boundary, Encoding encoding, int binaryBufferSize) { Files = new List <FilePart>(); Parameters = new Dictionary <string, ParameterPart>(); var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding, binaryBufferSize); string LogPath = @"C://MYSITE/LOG/"; string strConn = "Data Source=FILIPPO-PC;Initial Catalog=PLCCS_DB;Integrated Security=True"; DatabaseManagement db = new DatabaseManagement(strConn, LogPath); //db.NewErrorLog("Ho creato lo straming parser", DateTime.Now); streamingParser.ParameterHandler += parameterPart => Parameters.Add(parameterPart.Name, parameterPart); streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes) => { if (Files.Count == 0 || name != Files[Files.Count - 1].Name) { Files.Add(new FilePart(name, fileName, new MemoryStream(), type, disposition)); } Files[Files.Count - 1].Data.Write(buffer, 0, bytes); }; streamingParser.Run(); // Reset all the written memory streams so they can be read. foreach (var file in Files) { file.Data.Position = 0; } }
/// <summary> /// Parse the stream with the boundary, input encoding and buffer size. /// </summary> /// <param name="stream"> /// The stream containing the multipart data. /// </param> /// <param name="boundary"> /// The multipart/form-data boundary. This should be the value /// returned by the request header. /// </param> /// <param name="encoding"> /// The encoding of the multipart data. /// </param> /// <param name="binaryBufferSize"> /// The size of the buffer to use for parsing the multipart form data. This must be larger /// then (size of boundary + 4 + # bytes in newline). /// </param> /// <param name="binaryMimeTypes"> /// List of mimetypes that should be detected as file. /// </param> private void ParseStream(Stream stream, string boundary, Encoding encoding, int binaryBufferSize, string[] binaryMimeTypes) { var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding ?? Encoding.UTF8, binaryBufferSize, binaryMimeTypes); streamingParser.ParameterHandler += parameterPart => _parameters.Add(parameterPart); streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) => { if (partNumber == 0) { // create file with first partNo _files.Add(new FilePart(name, fileName, Utilities.MemoryStreamManager.GetStream($"{typeof(MultipartFormDataParser).FullName}.{nameof(ParseStream)}"), additionalProperties, type, disposition)); } Files[Files.Count - 1].Data.Write(buffer, 0, bytes); }; streamingParser.Run(); // Reset all the written memory streams so they can be read. foreach (var file in Files) { file.Data.Position = 0; } }
/// <summary> /// 处理多段表单数据 /// </summary> /// <param name="req"></param> /// <param name="parameterHandler"></param> /// <param name="fileHandler"></param> public static void HandleMultipartData(IOwinRequest req, StreamingMultipartFormDataParser.ParameterDelegate parameterHandler, StreamingMultipartFormDataParser.FileStreamDelegate fileHandler, StreamingMultipartFormDataParser.StreamClosedDelegate streamClosedDelegate = null) { if (!req.ContentType.StartsWith("multipart/form-data;")) throw new ArgumentException("'ContentType' not start with 'multipart/form-data;'."); var parser = new StreamingMultipartFormDataParser(req.Body); parser.ParameterHandler = parameterHandler; parser.FileHandler = fileHandler; parser.StreamClosedHandler = streamClosedDelegate; parser.Run(); }
/// <summary> /// Initializes a new instance of the <see cref="MultipartFormDataParser" /> class /// with the boundary, stream, input encoding and buffer size. /// </summary> /// <param name="stream"> /// The stream containing the multipart data /// </param> /// <param name="boundary"> /// The multipart/form-data boundary. This should be the value /// returned by the request header. /// </param> /// <param name="encoding"> /// The encoding of the multipart data /// </param> /// <param name="binaryBufferSize"> /// The size of the buffer to use for parsing the multipart form data. This must be larger /// then (size of boundary + 4 + # bytes in newline). /// </param> public MultipartFormDataParser(Stream stream, string boundary, Encoding encoding, int binaryBufferSize) { Files = new List<FilePart>(); Parameters = new List<ParameterPart>(); var streamingParser = new StreamingMultipartFormDataParser(stream, boundary, encoding, binaryBufferSize); streamingParser.ParameterHandler += parameterPart => Parameters.Add(parameterPart); streamingParser.FileHandler += (name, fileName, type, disposition, buffer, bytes) => { if (Files.Count == 0 || name != Files[Files.Count - 1].Name) { Files.Add(new FilePart(name, fileName, new MemoryStream(), type, disposition)); } Files[Files.Count - 1].Data.Write(buffer, 0, bytes); }; streamingParser.Run(); // Reset all the written memory streams so they can be read. foreach (var file in Files) { file.Data.Position = 0; } }