public async Task <bool> RegisterDocumento(DocumentoForRegister documentoForRegister) { using (var transaction = _context.Database.BeginTransaction()) { var documento = new Documento(); documento.fecha_registro = documentoForRegister.fecha_registro; documento.nombre = documentoForRegister.nombre; documento.numero_documento = documentoForRegister.numero_documento; documento.ruta = documentoForRegister.ruta; documento.tipo_id = documentoForRegister.tipo_id; documento.usuario_registro = documento.usuario_registro; documento.carga_id = documentoForRegister.carga_id; documento.site_id = documentoForRegister.site_id; try { await _context.AddRangeAsync(documento); await _context.SaveChangesAsync(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } //transaction.Commit(); return(true); } }
public async Task <IActionResult> UploadDocumentoSite(long siteId, int idtipo) { List <List <string> > valores = new List <List <string> >(); try { var ruta = _config.GetSection("AppSettings:UploadsDocuments").Value; var file = Request.Form.Files[0]; string name = file.FileName.Split('.')[0]; string folderName = siteId.ToString(); string webRootPath = ruta; string newPath = Path.Combine(webRootPath, folderName); if (name.Length > 26) { throw new ArgumentException("El nombre del file excede al lĂmite de caracteres permitidos (24 caracteres)"); } // name = name.Substring(0,25).ToString() ; var documento = new DocumentoForRegister { site_id = siteId , tipo_id = idtipo , numero_documento = "" , ruta = newPath , nombre = name + "." + file.FileName.Split('.')[1] , fecha_registro = DateTime.Now , usuario_registro = 1 }; byte[] fileData = null; using (var binaryReader = new BinaryReader(Request.Form.Files[0].OpenReadStream())){ fileData = binaryReader.ReadBytes(Request.Form.Files[0].ContentDisposition.Length); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (file.Length > 0) { string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string fullPath = Path.Combine(newPath, fileName); var checkextension = Path.GetExtension(fileName).ToLower(); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } } } var resp = await _repoBajaAltura.RegisterDocumento(documento); return(Ok(resp)); } catch (Exception ex) { return(Ok(ex.Message)); throw ex; } }