Example #1
0
        /// <summary>
        ///     Save all files in requests to specified directory.
        /// </summary>
        /// <param name="saveDir">The directory to place the file(s) in</param>
        /// <param name="fileRenamer">Function to rename the file(s)</param>
        /// <param name="maxSizeKb">The max total filesize allowed</param>
        /// <returns>Whether the file(s) was saved succesfully</returns>
        public async Task <bool> SaveFiles(string saveDir, Func <string, string> fileRenamer = null,
                                           long maxSizeKb = 4096)
        {
            if (UnderlyingRequest.HasFormContentType)
            {
                var form = await UnderlyingRequest.ReadFormAsync();

                if (form.Files.Sum(file => file.Length) > (maxSizeKb << 10))
                {
                    return(false);
                }

                foreach (var formFile in form.Files)
                {
                    var filename = fileRenamer == null ? formFile.FileName : fileRenamer(formFile.FileName);
                    var filepath = Path.Combine(saveDir, filename);
                    using (var filestream = File.Create(filepath))
                    {
                        await formFile.CopyToAsync(filestream);
                    }
                }

                return(true);
            }

            return(false);
        }
Example #2
0
 /// <summary>
 ///     Returns form-data from request, if any, null otherwise.
 /// </summary>
 public async Task <IFormCollection> GetFormDataAsync()
 {
     if (!UnderlyingRequest.HasFormContentType)
     {
         return(null);
     }
     return(await UnderlyingRequest.ReadFormAsync());
 }
Example #3
0
        /// <summary>
        ///     Returns form-data from request, if any, null otherwise.
        /// </summary>
        public async Task <IFormCollection> GetFormDataAsync()
        {
            if (!UnderlyingRequest.HasFormContentType)
            {
                return(null);
            }

            if (_form != null)
            {
                return(_form);
            }

            _form = await UnderlyingRequest.ReadFormAsync();

            return(_form);
        }