public async Task <IActionResult> FileUpload(FundingRequestViewModel request, List <IFormFile> files) { //first lets verify we have a file and that it is the correct type foreach (IFormFile formFile in files) { string ext = Path.GetExtension(formFile.FileName); if (formFile.Length < 0 || ext != ".pdf") { TempData["ErrorMessage"] = "Files must be in pdf format."; return(RedirectToAction(nameof(FundingRequestForm))); } } //build path to pdf folder string dir = Path.Combine("E:\\ApplicationDocuments\\FundingRequests\\BudgetExplanations\\", request.CapFundingRequest.ProjectName + "_" + request.CapFundingRequest.Id); //create directory by default if one already exists it will ignore the line Directory.CreateDirectory(dir); int requestId = request.CapFundingRequest.Id; //Now its time to copy the files to the directory foreach (IFormFile formFile in files) { //get file size to make sure one is there long size = files.Sum(f => f.Length); string fileName = Path.GetFileName(formFile.FileName); string filePath = Path.Combine(dir, fileName); using (FileStream stream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(stream); FundingRequestAttachments attachments = new FundingRequestAttachments { FileName = fileName, CapFundingRequestId = requestId, FileLocation = filePath }; await _context.FundingRequestAttachments.AddAsync(attachments); } } await _context.SaveChangesAsync(); CapFundingRequest objRequest = await _context.CapFundingRequests.SingleOrDefaultAsync(r => r.Id == requestId); object viewModel = await CreateViewModel(objRequest); return(View("FundingRequestReview", viewModel)); }
public async Task <IActionResult> DeleteAttachment(int?id) { if (id == null) { return(NotFound()); } FundingRequestAttachments attachment = await _context.FundingRequestAttachments .SingleOrDefaultAsync(r => r.Id == id); if (attachment == null) { return(NotFound()); } return(View(attachment)); }
public async Task <IActionResult> DelAttachmentConfirmed(int id) { FundingRequestAttachments attachment = await _context.FundingRequestAttachments.SingleOrDefaultAsync(a => a.Id == id); //hold onto the parent request id so we can redirect to the correct form CapFundingRequest request = await _context.CapFundingRequests.SingleOrDefaultAsync(r => r.Id == attachment.CapFundingRequestId); //get the location of the file to delete string fileLocation = attachment.FileLocation; System.IO.File.Delete(fileLocation); _context.FundingRequestAttachments.Remove(attachment); await _context.SaveChangesAsync(); FundingRequestViewModel viewModel = new FundingRequestViewModel { CapFundingRequest = request, FundingRequestAttachments = _context.FundingRequestAttachments.Where(a => a.CapFundingRequestId == request.Id), }; return(View("SupportingDocuments", viewModel)); }
//display attached files in browser public IActionResult GetFile(int?id) { FundingRequestAttachments file = _context.FundingRequestAttachments.SingleOrDefault(f => f.Id == id); return(new PhysicalFileResult(file.FileLocation, "Application/pdf")); }