public async Task <IActionResult> PutxChepter([FromRoute] Guid id, [FromBody] XChepter varxChepter) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != varxChepter.XChepterId) { return(BadRequest()); } _context.Entry(varxChepter).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!xChepterExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PostxChepter([FromBody] XChepter varxChepter) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.XChepter.Add(varxChepter); await _context.SaveChangesAsync(); return(CreatedAtAction("GetxChepter", new { id = varxChepter.XChepterId }, varxChepter)); }
public async Task <IActionResult> Upload() { Guid id; try { id = new Guid(Request.Form["rowid"]); } catch { return(NoContent()); } XChepter xi = _context.XChepter.FirstOrDefault(x => x.XChepterId == id); if (xi != null) { if (Request.Form.Files.Count > 0) { IFormFile file = Request.Form.Files[0]; if (file == null || file.Length == 0) { return(NoContent()); } // try to delete old file if (xi.refFile != null && xi.refFile.Length > 20) // guid-name { if (System.IO.File.Exists(FilePath + xi.refFile)) { try { System.IO.File.Delete(FilePath + xi.refFile); } catch { } } } // handle file here var stream = file.OpenReadStream(); FileInfo fi; string realFileName; fi = new FileInfo(file.FileName); Guid rowid = Guid.NewGuid(); realFileName = rowid.ToString().Replace("{", "").Replace("}", "").Replace("-", "") + fi.Extension; string targetFilePath = FilePath + realFileName; using (var targetStream = System.IO.File.Create(targetFilePath)) { await stream.CopyToAsync(targetStream); stream.Close(); targetStream.Close(); } xi.refFile = "/" + _configuration["fileStoragePath"] + "/" + realFileName; await _context.SaveChangesAsync(); return(Ok(xi.refFile)); } } return(NotFound()); }