public IHttpActionResult uploadPhoto(int productionId) { var dbcontext = new BroadwayBuilderContext(); var productionService = new ProductionService(dbcontext); //try to upload pdf and save to server filesystem try { //get the content, headers, etc the full request of the current http request var httpRequest = HttpContext.Current.Request; var fileCollection = httpRequest.Files; var fileValidator = new FileValidator(); int MaxContentLength = 1 * 1024 * 1024 * 5; //Size = 5 MB var validExtensions = new List <string>() { ".jpg" }; int maxFileCount = 5; var validationResult = fileValidator.ValidateFiles(fileCollection, validExtensions, MaxContentLength, maxFileCount); if (!validationResult.ValidationSuccessful) { var errorMessage = string.Join("\n", validationResult.Reasons); return(BadRequest(errorMessage)); } // Used for loop since foreach did not handle cycling through multiple files well. for (int i = 0; i < httpRequest.Files.Count; i++) { // Grab current file of the request HttpPostedFileBase putFile = new HttpPostedFileWrapper(httpRequest.Files[i]); // Send to production service where functinality to save the file is productionService.SavePhoto(productionId, putFile); } return(Ok("Photo Uploaded")); } catch (Exception ex) // Todo: log error { return(BadRequest(ex.Message)); } }
public IHttpActionResult UploadProductionProgram(int productionId) { var dbcontext = new BroadwayBuilderContext(); var productionService = new ProductionService(dbcontext); // Try to upload pdf and save to server filesystem try { // Get the full request of the current http request var httpRequest = HttpContext.Current.Request; var fileCollection = httpRequest.Files; var fileValidator = new FileValidator(); int MaxContentLength = 1024 * 1024 * 1; int maxFileCount = 1; var extensions = new List <string>() { ".pdf" }; // Validate files for valid extension, and size var validationResult = fileValidator.ValidateFiles(fileCollection, extensions, MaxContentLength, maxFileCount); if (!validationResult.ValidationSuccessful) { var errorMessage = string.Join("\n", validationResult.Reasons); return(BadRequest(errorMessage)); } // Send file to be saved to server foreach (string filename in fileCollection) { HttpPostedFileBase putFile = new HttpPostedFileWrapper(fileCollection[filename]); productionService.SaveProgram(productionId, putFile); } return(Ok("Pdf Uploaded")); } catch (Exception e) { // Todo: log error // Todo: add proper error handling in production service return(BadRequest(e.Message)); } }
public IHttpActionResult UploadResume(int userId) { //A list in case we want to accept more than one file type List <string> allowedFileExtension = new List <string> { ".pdf" }; //Business Rule - only one file allowed to submit int maxFileCount = 1; // Max file size is 1MB const int maxContentLength = 1024 * 1024 * 1; try { //get the content, headers, etc the full request of the current http request var httpRequest = HttpContext.Current.Request; var fileValidator = new FileValidator(); //Validate the submitted file to verify that it complies with Business Rules var validationResult = fileValidator.ValidateFiles(httpRequest.Files, allowedFileExtension, maxContentLength, maxFileCount); if (!validationResult.ValidationSuccessful)//if one or more business rules were violated { var errorMessage = string.Join("\n", validationResult.Reasons); return(Content((HttpStatusCode)406, errorMessage)); } // Grab current file of the request var postedFile = httpRequest.Files[0]; using (var dbContext = new BroadwayBuilderContext()) { var userService = new UserService(dbContext); User user = userService.GetUser(userId); if (user == null)//check if user exists { return(Content((HttpStatusCode)404, "There is no record of that User.")); } var resumeService = new ResumeService(dbContext); Resume resume = resumeService.GetResumeByUserID(userId); if (resume == null)//check if user has already submitted a resume { Resume userResume = new Resume(userId, Guid.NewGuid()); resumeService.CreateResume(userResume); var result = dbContext.SaveChanges(); if (result <= 0) { return(Content((HttpStatusCode)500, "Failed to add a resume onto our database")); } resume = userResume; } //Folder path of the user var subdir = Path.Combine(ConfigurationManager.AppSettings["ResumeDir"], (resume.ResumeGuid.ToString() + "/")); //@"C:\Resumes\"+resume.ResumeGuid; //Filepath of the submitted file var filePath = Path.Combine(subdir, resume.ResumeGuid.ToString() + ".pdf"); // subdir+@"\"+resume.ResumeGuid+".pdf"; if (!Directory.Exists(subdir)) //check if the directory exists { Directory.CreateDirectory(subdir); //create the directory if it doesnt exist } //saves file onto the specified file path and overwrites any file that may exist in that shares the same path postedFile.SaveAs(filePath); return(Content((HttpStatusCode)200, "File Uploaded")); } } catch (HttpException e)//HttpPostedFile.SaveAs exception { return(Content((HttpStatusCode)500, "Unable to save the file onto our file system.")); } catch (IOException e)//Exception thrown when creating directory { return(Content((HttpStatusCode)500, "Unable to delete the job posting")); } catch (DbUpdateException e)//exception thrown while saving the database { return(Content((HttpStatusCode)500, "Unable to delete the job posting")); } catch (DbEntityValidationException dbEntityValidationException) { return(Content((HttpStatusCode)500, "Unable to delete the job posting")); } catch (Exception e) { return(Content((HttpStatusCode)400, e.Message)); } }