public ProjectController(APIContext context) { objProject = new ProjectDataAccessLayer(context); if (context.Project.Count() == 0) { context.Project.Add(new Project { Nom = "Item1" }); context.SaveChanges(); } }
public async Task <IActionResult> UploadDocument(int DocumentID, IFormFile DocFileName) { string strFileDestinationPath; string strNewFilename; int intDocumentID = 137; // To Do: // Someone pressed the upload button and there are no files selected. // What if someone already has the document open - i.e they've gone on the file server and are editing it outside of the application. ProjectDataAccessLayer _pdal = new ProjectDataAccessLayer(); Document FileDoc = new Document(); FileDoc = objDocument.GetDocumentData(intDocumentID); // TO DO - Add validation and checking at a later date - Gavin 12-Jan-2018 if (ModelState.IsValid) { strFileDestinationPath = _pdal.GetFileLocation(FileDoc.ProjectID); // Get the filename - entered by the user string ext = Path.GetExtension(DocFileName.FileName.ToString()); // Get the filename - as expected by the database strNewFilename = strFileDestinationPath + "\\" + FileDoc.DocumentNo + ".R" + FileDoc.CurrentRev + ext; // Get the destination file path. using (var stream = new FileStream(strNewFilename, FileMode.Create)) { // Note that any matching documents in the directory will be overwritten automatically - Gav 30-Jan-2018 await DocFileName.CopyToAsync(stream); stream.Close(); } // Grab the filename and update the document record with it. objDocument.FileUpload(DocumentID, FileDoc.DocumentNo + ".R" + FileDoc.CurrentRev + ext); } //To Do Needs a bit of work - should open the edit page and fill out the fields - all it does at the moment is open the form without making any changes. Document EditDocData = new Document(); EditDocData = objDocument.GetDocumentData(DocumentID); return(RedirectToAction("EditDocument")); }
public async Task <IActionResult> UploadAttachment(int DocumentID, IFormFile AttachmentFileName) { string strFileDestinationPath; string strNewFilename; ProjectDataAccessLayer _pdal = new ProjectDataAccessLayer(); Document FileDoc = new Document(); FileDoc = objDocument.GetDocumentData(DocumentID); // TO DO - Add validation and checking at a later date - Gavin 12-Jan-2018 if (ModelState.IsValid) { strFileDestinationPath = _pdal.GetFileLocation(FileDoc.ProjectID); // Get the filename - entered by the user string ext = Path.GetExtension(AttachmentFileName.FileName.ToString()); // Get the filename - as expected by the database strNewFilename = strFileDestinationPath + "\\" + FileDoc.DocumentNo + ".R" + FileDoc.CurrentRev + "-attachment" + ext; // Get the destination file path. using (var stream = new FileStream(strNewFilename, FileMode.Create)) { await AttachmentFileName.CopyToAsync(stream); } // Grab the Attachmentname and update the document record with it. objDocument.AttachmentUpload(DocumentID, FileDoc.DocumentNo + ".R" + FileDoc.CurrentRev + "-attachment" + ext); } //To Do Needs a bit of work - should open the edit page and fill out the fields - all it does at the moment is open the form without making any changes. Document EditDocData = new Document(); EditDocData = objDocument.GetDocumentData(DocumentID); return(RedirectToAction("EditDocument")); }
// Download file should take the source path and filename from the public async Task <IActionResult> DownloadDocument(int DocumentID) { Document FileDoc = new Document(); FileDoc = objDocument.GetDocumentData(DocumentID); ProjectDataAccessLayer _pdal = new ProjectDataAccessLayer(); string strSourcePath = _pdal.GetFileLocation(FileDoc.ProjectID); string strFileDestinationPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string strFileSourceName = strSourcePath + "\\" + FileDoc.DocFileName; var memory = new MemoryStream(); using (var stream = new FileStream(strFileSourceName, FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; //return File(memory, FileDoc.GetContentType(strFileSourceName), strFileDestinationPath + "\\" + FileDoc.DocFileName); return(File(memory, FileDoc.GetContentType(strFileSourceName), FileDoc.DocFileName)); }