/// <summary> /// Adds file/s from the http request /// </summary> /// <param name="predicate">Set this param if you want to read a file from a specific form field name.</param> /// <param name="formFileRules">The rules to apply to the file uploaded.</param> /// <returns></returns> public static async Task <IApiResponse <IEnumerable <UserBlobs> > > CreateRangeUserBlobsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IFormFileCollection files, Func <IFormFile, bool> predicate, FormFileRules formFileRules, IBlobStorage blobStorage, string userId, string tableName, string controllerName = null, string actionName = null, string remoteIpAddress = null) { var any = predicate == null?files.Any() : files.Any(predicate); if (any) { await dbContext.CreateAuditrailsAsync(new UserBlobs(), "No file/s uploaded", userId, controllerName, actionName, remoteIpAddress); return(new ApiResponse <IEnumerable <UserBlobs> >() { Status = ApiResponseStatus.Failed, Message = "FileIsNullOrEmpty" }); } if (any && formFileRules != null) { var isValidFormFileApiResponse = files.IsValidFormFileApiResponse(predicate, formFileRules); if (isValidFormFileApiResponse.Status == ApiResponseStatus.Failed) { return(new ApiResponse <IEnumerable <UserBlobs> >() { Status = ApiResponseStatus.Failed, Message = isValidFormFileApiResponse.Message }); } } var createdOn = DateTime.Now; IEnumerable <IFormFile> formFiles = predicate == null?files.ToList() : files.Where(predicate); IList <UserBlobs> uploadedUserBlobs = new List <UserBlobs>(); foreach (var file in formFiles) { var fileExtension = Path.GetExtension(file.FileName); var uniqueFileName = Guid.NewGuid().ToString("N") + string.Empty.NewNumericIdentifier().ToString(); var filename = $"{uniqueFileName}{fileExtension}"; await blobStorage.WriteAsync(filename, file.OpenReadStream()); var userBlob = new UserBlobs().AutoMap(file); userBlob.FilePath = filename; userBlob.CreatedBy = userId; userBlob.CreatedOn = createdOn; userBlob.TableName = tableName; uploadedUserBlobs.Add(userBlob); await dbContext.AddAuditedAsync(userBlob, "File was uploaded", userId, controllerName, actionName, remoteIpAddress); } var result = await dbContext.SaveChangesAsync(); return(result.TransactionResultApiResponse(uploadedUserBlobs.AsEnumerable())); }