private string ProcessUploadedDocument(BugsCreateViewModel model) { string uniqueFileName = null; if (model.Document != null) //if document property is not null - user selected a file { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "documents"); //To get to the documents folder we will use the Path class to combine wwwroot and the documents folders uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Document.FileName; //We use the Guid class to ensure the uploaded files are unique string filePath = Path.Combine(uploadsFolder, uniqueFileName); //Combine uploadsFolders and the uniqueFileName model.Document.CopyTo(new FileStream(filePath, FileMode.Create)); //Copy the uploaded file and create in the server } return(uniqueFileName); }
//public ActionResult Create([Bind("Id,ProjectId,BugPriorityId,BugStatusId,BugCreatedBy,BugCreatedOn,BugClosedBy,BugClosedOn,BugResolutionSummary,Document")] BugsCreateViewModel model) //{ // try // { // if (ModelState.IsValid) // { // string uniqueFileName = ProcessUploadedDocument(model); //Calling the method ProcessUploadedDocuments // //Create new Bugs object and copy the bug related properties from the model object to it // Bugs newBug = new Bugs // { // ProjectId = model.ProjectId, // BugPriorityId = model.BugPriorityId, // BugStatusId = model.BugStatusId, // BugCreatedBy = model.BugCreatedBy, // BugCreatedOn = model.BugCreatedOn, // BugClosedBy = model.BugClosedBy, // BugClosedOn = model.BugClosedOn, // BugResolutionSummary = model.BugResolutionSummary, // Attachment = uniqueFileName // }; // bugRepository.Add(newBug); // return RedirectToAction(nameof(Index)); // } // return View(); // } // catch // { // return NotFound(); // } //} public ActionResult Create([Bind("Id,ProjectId,BugPriorityId,BugStatusId,BugCreatedBy,BugCreatedOn,BugClosedBy,BugClosedOn,BugResolutionSummary,Document")] BugsCreateViewModel bug) { HttpClient Client = api.Initial(); //HTTP POST var postTask = Client.PostAsJsonAsync <BugsCreateViewModel>("api/bugs", bug); //postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { return(RedirectToAction("Index")); } return(View()); }