public async Task <ResponseBase> InsertarImagenPromocion(Imagen_promocion imgp) { var response = new ResponseBase(); try { using (var connection = new SqlConnection(con.getConnection())) { using (var command = new SqlCommand("Catalogos.spInsertaImagenPromocion", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Clear(); command.Parameters.AddWithValue("@idImagenPromocion", 0); command.Parameters.AddWithValue("@idEmpresa", imgp.idEmpresa); command.Parameters.AddWithValue("@imagen", imgp.imagen); command.Parameters.AddWithValue("@url", imgp.url); command.Parameters.AddWithValue("@idPromocion", imgp.idPromocion); command.Parameters["@idImagenPromocion"].Direction = ParameterDirection.Output; connection.Open(); var result = await command.ExecuteNonQueryAsync(); if (result > 0) { response.id = Convert.ToInt32(command.Parameters["@idImagenPromocion"].Value); response.success = true; response.message = "Datos Insertados Correctamente"; } } } } catch (Exception ex) { response.success = false; response.message = ex.Message; } return(response); }
public async Task <ActionResult> ImagenProducto(IFormCollection formdata) { var response = new ResponseBase(); var imgp = new Imagen_promocion(); List <string> fileExtension = new List <string>() { ".png", ".jpg", ".jpeg" }; try { imgp.idPromocion = int.Parse(formdata["idPromocion"]); var idEmpresa = formdata["idEmpresa"]; var files = HttpContext.Request.Form.Files; foreach (var file in files) { var filename = file.FileName; var ext = Path.GetExtension(filename); var buffer = file.Length; double mb = (buffer / 1024f) / 1024f; if (mb > 10) { response.success = false; response.message = "tamaño de archivo demasiado grande"; return(StatusCode(500, response)); } if (!fileExtension.Contains(ext)) { response.success = false; response.message = "extension de archivo no permitida"; return(StatusCode(500, response)); } if (file.Length > 0) { var filee = Path.Combine(environment.ContentRootPath); var date = DateTime.Now; var id = date.Millisecond; var carpeta = filee + "\\" + "Content" + "\\" + "empresa" + "\\" + idEmpresa + "\\" + "promociones" + "\\" + imgp.idPromocion; var filePath = filee + "\\" + "Content" + "\\" + "empresa" + "\\" + idEmpresa + "\\" + "promociones" + "\\" + imgp.idPromocion + "\\" + id + ext; var ruta = "\\" + "Content" + "\\" + "empresa" + "\\" + idEmpresa + "\\" + "promociones" + "\\" + imgp.idPromocion + "\\" + id + ext; imgp.url = ruta; imgp.idEmpresa = int.Parse(idEmpresa); imgp.imagen = id.ToString() + ext; if (!Directory.Exists(carpeta)) { Directory.CreateDirectory(carpeta); } using (var stream = System.IO.File.Create(filePath)) { await file.CopyToAsync(stream); response = await this.promocion.InsertarImagenPromocion(imgp); if (response.success == false) { return(StatusCode(403, response)); } } } } } catch (Exception ex) { response.success = false; response.message = ex.Message; return(StatusCode(500, response)); } return(Ok(response)); }