public async Task <Response> CrearFichero(NoticiaTransfer file) { InicializarMensaje(null); Response response = new Response(); try { response = await apiServicio.InsertarAsync(file, new Uri(WebApp.BaseAddress), "api/Noticias/UploadFiles"); if (response.IsSuccess) { var responseLog = await GuardarLogService.SaveLogEntry(new LogEntryTranfer { ApplicationName = Convert.ToString(Aplicacion.WebAppTh), ExceptionTrace = null, Message = "Se ha subido un archivo", UserName = "******", LogCategoryParametre = Convert.ToString(LogCategoryParameter.Create), LogLevelShortName = Convert.ToString(LogLevelParameter.ADV), EntityID = string.Format("{0} {1}", "Noticia:", file.Titulo), }); return(new Response { IsSuccess = true, Message = response.Message, Resultado = response.Resultado }); } ViewData["Error"] = response.Message; return(new Response { IsSuccess = false, Message = response.Message, }); } catch (Exception ex) { await GuardarLogService.SaveLogEntry(new LogEntryTranfer { ApplicationName = Convert.ToString(Aplicacion.WebAppTh), Message = "Subiendo archivo", ExceptionTrace = ex.Message, LogCategoryParametre = Convert.ToString(LogCategoryParameter.Create), LogLevelShortName = Convert.ToString(LogLevelParameter.ERR), UserName = "******" }); return(new Response { IsSuccess = false, Message = response.Message, }); } }
public async Task <ActionResult> Create(NoticiaViewModel view, List <IFormFile> files) { if (files.Count > 0) { byte[] data; using (var br = new BinaryReader(files[0].OpenReadStream())) data = br.ReadBytes((int)files[0].OpenReadStream().Length); var noticiatransfer = new NoticiaTransfer { Titulo = view.Titulo, Fecha = view.Fecha, Descripcion = view.Descripcion, Fichero = data, }; var respuesta = await CrearFichero(noticiatransfer); Noticia onoticia = JsonConvert.DeserializeObject <Noticia>(respuesta.Resultado.ToString()); if (respuesta.IsSuccess) { await uploadFileService.UploadFile(noticiatransfer.Fichero, "Noticias", Convert.ToString(onoticia.IdNoticia), "jpg"); onoticia.Foto = string.Format("{0}/{1}.{2}", "Noticias", Convert.ToString(onoticia.IdNoticia), "jpg"); return(RedirectToAction("Index")); } else { ViewData["Error"] = respuesta.Message; var noticia = new Noticia { Titulo = view.Titulo, }; InicializarMensaje(null); return(View(noticia)); } } return(BadRequest()); }
public NoticiaTransfer GetFile(string folder, string fileName, string extension) { var a = string.Format("{0}/{1}{2}", folder, fileName, extension); var targetDirectory = Path.Combine(_hostingEnvironment.WebRootPath, a); var file = new FileStream(targetDirectory, FileMode.Open); byte[] data; using (var br = new BinaryReader(file)) data = br.ReadBytes((int)file.Length); var documenttransfer = new NoticiaTransfer { Titulo = "", Fichero = data, }; return(documenttransfer); }
public async Task <Response> Post([FromBody] NoticiaTransfer noticiaTransfer) { try { if (!ModelState.IsValid) { return(new Response { IsSuccess = false, Message = "" }); } var noticia = new Noticia { Titulo = noticiaTransfer.Titulo, Fecha = noticiaTransfer.Fecha, Descripcion = noticiaTransfer.Descripcion, }; var respuesta = Existe(noticia); if (!respuesta.IsSuccess) { Noticia objetoNoticia = await InsertarNoticia(noticia); await uploadFileService.UploadFile(noticiaTransfer.Fichero, "Noticias", Convert.ToString(objetoNoticia.IdNoticia), "jpg"); var seleccionado = db.Noticia.Find(objetoNoticia.IdNoticia); seleccionado.Foto = string.Format("{0}/{1}.{2}", "Noticias", Convert.ToString(objetoNoticia.IdNoticia), "jpg"); db.Noticia.Update(seleccionado); db.SaveChanges(); return(new Response { IsSuccess = true, Message = Mensaje.Satisfactorio, Resultado = objetoNoticia }); } return(new Response { IsSuccess = false, Message = Mensaje.ExisteRegistro }); } catch (Exception ex) { await GuardarLogService.SaveLogEntry(new LogEntryTranfer { ApplicationName = Convert.ToString(Aplicacion.SwTH), ExceptionTrace = ex.Message, Message = Mensaje.Excepcion, LogCategoryParametre = Convert.ToString(LogCategoryParameter.Critical), LogLevelShortName = Convert.ToString(LogLevelParameter.ERR), UserName = "", }); return(new Response { IsSuccess = false, Message = Mensaje.Error, }); } }