Frends.Community.Multipart/Frends.Community.Multipart.cs
/// <summary>
/// A frends task form parsing multipart/form-data requests.
/// Documentation: https://github.com/CommunityHiQ/Frends.Community.Multipart
/// </summary>
/// <param name="input">What to repeat.</param>
/// <param name="cancellationToken"></param>
///  <returns>Object { List&lt;{ string Name, string Value }&gt; Parameters, List&lt;{ string Name, byte[] Contents }&gt; Files }</returns>
A frends task form parsing multipart/form-data requests.
A frends task form parsing multipart / form - data requests .
public static async Task<Result> ParseMultipartRequest(Input input, CancellationToken cancellationToken)
        {
            var ret = new List<File>();
            var ret2 = new List<Parameter>();
            using (var stream = new MemoryStream(input.ByteArray))
            {
                var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);
                foreach (var file in parser.Files)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var tmp = new File {Name = file.FileName, Contents = ((MemoryStream) file.Data).ToArray()};
                    ret.Add(tmp);
                }
                foreach (var param in parser.Parameters)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var retParam = new Parameter
                    {
                        Name = param.Name,
                        Value = param.Data
                    };
                    ret2.Add(retParam);
                }
            }
            var output = new Result
            {
                Files = ret,
                Parameters = ret2
            };
            return output;
        }
public static async Task < Result > ParseMultipartRequest ( Input input , CancellationToken cancellationToken ) { var ret = new List < File > ( ) ; var ret2 = new List < Parameter > ( ) ; using ( var stream = new MemoryStream ( input . ByteArray ) ) { var parser = await MultipartFormDataParser . ParseAsync ( stream ) . ConfigureAwait ( false ) ; foreach ( var file in parser . Files ) { cancellationToken . ThrowIfCancellationRequested ( ) ; var tmp = new File { Name = file . FileName , Contents = ( ( MemoryStream ) file . Data ) . ToArray ( ) } ; ret . Add ( tmp ) ; } foreach ( var param in parser . Parameters ) { cancellationToken . ThrowIfCancellationRequested ( ) ; var retParam = new Parameter { Name = param . Name , Value = param . Data } ; ret2 . Add ( retParam ) ; } } var output = new Result { Files = ret , Parameters = ret2 } ; return output ; }
A frends task form parsing multipart/form-data requests.
A frends task form parsing multipart / form - data requests .
Object { List<{ string Name, string Value }> Parameters, List<{ string Name, byte[] Contents }> Files }
Object { List< { string Name string Value } > Parameters List< { string Name byte [] Contents } > Files }
input
Input
None
cancellationToken
CancellationToken
None