// --------------------------------------------------------- public async Task <HdArchivoVM> Add(HdArchivoCreateVM model) { try { HdArchivo hdarchivo = new HdArchivo { hd_doc_id = model.hd_doc_id, descripcion = model.descripcion.Left(150), nombrefile = model.nombrefile.Left(250), f_crea = DateTime.Now, usuario_id = model.usuario_id }; await _context.HdArchivos.AddAsync(hdarchivo); await _context.SaveChangesAsync(); var regreso = await Get(hdarchivo.hd_archivo_id); return(regreso); } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> UploadUnFile() { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var file = Request.Form.Files[0]; if (file.Length <= 0) { throw new Exception("No envio un archivo"); } string descripcion = HttpContext.Request.Form["descripcion"].ToString(); string IdDocStr = HttpContext.Request.Form["hd_doc_id"].ToString(); string Idstr = HttpContext.GetClaim("usuarioId"); int usuario_id = Idstr.TrueInt(); int IdDoc = IdDocStr.TrueInt(); string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); fileName = GetUniqueFileName(fileName); HdArchivoCreateVM modelo = new HdArchivoCreateVM { hd_doc_id = IdDoc, descripcion = descripcion, nombrefile = fileName, usuario_id = usuario_id }; var regreso = await _servicioArchivo.Add(modelo); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/imagenes", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(fileStream); fileStream.Flush(); } scope.Complete(); return(Ok(regreso)); } }