public ViewResult Get(PhotoManagementPostRequest request) { if (request.File == null) { // Make sure a file has been uploaded. var returnValue = photoManagementService.Get(new PhotoManagementRequest { LocalEducationAgencyId = request.LocalEducationAgencyId }); returnValue.ErrorMessages = new List<string> { "Please select a file to upload." }; returnValue.SuccessfullyProcessedPhotos = 0; returnValue.TotalRecords = 0; return View(returnValue); } var contentRange = ContentRange(); var fileCollection = request.File as IEnumerable<HttpPostedFileBase>; var file = fileCollection.ElementAt(0); var fileBytes = new byte[file.InputStream.Length]; var cacheKey = GetCacheKey(request.LocalEducationAgencyId, file.FileName); file.InputStream.Read(fileBytes, 0, (int)file.InputStream.Length); if (contentRange > 0) { // This file is being uploaded in chunks. Take this first chunk and add it to whatever else is in cache. // Then compare it against the content range. If the total file bytes are the same as the content range, // then start the processing of the file. Otherwise, return null. var existingFileBytes = sessionStateProvider[cacheKey] as byte[] ?? new byte[0]; fileBytes = existingFileBytes.Concat(fileBytes).ToArray(); if (fileBytes.Length == contentRange) { // Remove this from the cache and process the file. sessionStateProvider.RemoveValue(cacheKey); } else if (fileBytes.Length > contentRange) { // Something is wrong. This should never happen. Get rid of the file and // start over. sessionStateProvider.RemoveValue(cacheKey); return View((object)false); } else { // This is just part of a larger file. Store it for later. sessionStateProvider.SetValue(cacheKey, fileBytes); return View((object)false); } } // The full file has been uploaded. Process it. request.File = fileBytes; return View(photoManagementService.Post(request)); }
protected override void ExecuteTest() { var request = new PhotoManagementPostRequest { SchoolIdToLoadImagesTo = 123, // Incorrect school Id. LocalEducationAgencyId = LocalEducationAgencyId }; photoManagementModel = photoManagementService.Post(request); }