Created
May 28, 2025 20:50
-
-
Save Giovasdf/3084001fdc7477a7001bdad5debabf2c to your computer and use it in GitHub Desktop.
Upload Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Dapper; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.Formatters; | |
| using Microsoft.Data.SqlClient; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.IO; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNetCore.Http; | |
| using Microsoft.Extensions.FileProviders; | |
| [HttpPost("Actualiza")] | |
| public async Task<IActionResult> Actualiza([FromForm] IFormCollection form) | |
| { | |
| try | |
| { | |
| // 1. Obtener campos básicos | |
| var idOppa = Guid.Parse(form["idOppa"]); | |
| var rut = form["rut"]; | |
| var email = form["email"]; | |
| var nombre = form["nombre"]; | |
| var fechaNacimiento = form["fecha_nacimiento"]; | |
| var password = form["password"]; | |
| var estado = form["estado"]; | |
| var direccion = form["direccion"]; | |
| // 2. Función para guardar archivo y retornar ruta relativa | |
| string SaveFile(IFormFile file, string folderName) | |
| { | |
| var uploadsRoot = Path.Combine(Directory.GetCurrentDirectory(), "uploads", folderName); | |
| Directory.CreateDirectory(uploadsRoot); // asegúrate que la carpeta existe | |
| var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); | |
| var filePath = Path.Combine(uploadsRoot, fileName); | |
| using (var stream = new FileStream(filePath, FileMode.Create)) | |
| { | |
| file.CopyTo(stream); | |
| } | |
| // Esto retorna la ruta pública relativa | |
| return $"/uploads/{folderName}/{fileName}"; | |
| } | |
| // 3. Guardar archivos si se subieron | |
| var curriculumPath = form.Files["curriculum"] != null ? SaveFile(form.Files["curriculum"], "curriculum") : null; | |
| var antecedentesPath = form.Files["antecedentes"] != null ? SaveFile(form.Files["antecedentes"], "antecedentes") : null; | |
| var carnetPath = form.Files["carnet"] != null ? SaveFile(form.Files["carnet"], "carnet") : null; | |
| var fotoPath = form.Files["foto"] != null ? SaveFile(form.Files["foto"], "foto") : null; | |
| // 4. Encriptar password si viene | |
| if (!string.IsNullOrEmpty(password)) | |
| { | |
| password = GestorPassword.DefinePwd(password); | |
| } | |
| // 5. Hacer update | |
| var sql = @" | |
| UPDATE oppa SET | |
| rut = @rut, | |
| email = @email, | |
| nombre = @nombre, | |
| fecha_nacimiento = @fechaNacimiento, | |
| password = @password, | |
| estado = @estado, | |
| direccion = @direccion, | |
| curriculum = COALESCE(@curriculum, curriculum), | |
| antecedentes = COALESCE(@antecedentes, antecedentes), | |
| carnet = COALESCE(@carnet, carnet), | |
| foto = COALESCE(@foto, foto) | |
| WHERE idOppa = @idOppa"; | |
| c.Execute(sql, new | |
| { | |
| idOppa, | |
| rut, | |
| email, | |
| nombre, | |
| fechaNacimiento, | |
| password, | |
| estado, | |
| direccion, | |
| curriculum = curriculumPath, | |
| antecedentes = antecedentesPath, | |
| carnet = carnetPath, | |
| foto = fotoPath | |
| }); | |
| return Ok(new { Message = "Actualización exitosa" }); | |
| } | |
| catch (Exception ex) | |
| { | |
| return BadRequest(new { Message = "Error al actualizar: " + ex.Message }); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNetCore.Builder; | |
| using Microsoft.AspNetCore.Hosting; | |
| using Microsoft.AspNetCore.HttpsPolicy; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.Extensions.Configuration; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Hosting; | |
| using Microsoft.Extensions.Logging; | |
| using Microsoft.OpenApi.Models; | |
| using Microsoft.Extensions.FileProviders; | |
| using System.IO; | |
| namespace ApiOppa | |
| { | |
| public class Startup | |
| { | |
| public Startup(IConfiguration configuration) | |
| { | |
| Configuration = configuration; | |
| } | |
| public IConfiguration Configuration { get; } | |
| // This method gets called by the runtime. Use this method to add services to the container. | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddCors(options => | |
| { | |
| options.AddPolicy("lala", | |
| builder => | |
| { | |
| builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); | |
| }); | |
| }); | |
| services.AddControllers(); | |
| services.AddSwaggerGen(); | |
| services.AddSwaggerGen(c => | |
| { | |
| c.SwaggerDoc("v1", new OpenApiInfo | |
| { | |
| Version = "v1", | |
| Title = "Api OPPA", | |
| Description = "Api Oppa m�todos para la web y la app", | |
| }); | |
| }); | |
| } | |
| // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
| public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
| { | |
| if (env.IsDevelopment()) | |
| { | |
| app.UseDeveloperExceptionPage(); | |
| } | |
| app.UseHttpsRedirection(); | |
| // Habilitar archivos estáticos | |
| app.UseStaticFiles(); | |
| // Habilitar carpeta /uploads como ruta pública | |
| var uploadsPath = Path.Combine(Directory.GetCurrentDirectory(), "uploads"); | |
| if (!Directory.Exists(uploadsPath)) | |
| { | |
| Directory.CreateDirectory(uploadsPath); | |
| } | |
| app.UseStaticFiles(new StaticFileOptions | |
| { | |
| FileProvider = new PhysicalFileProvider(uploadsPath), | |
| RequestPath = "/uploads" | |
| }); | |
| app.UseRouting(); | |
| app.UseCors("lala"); | |
| app.UseAuthorization(); | |
| app.UseSwagger(); | |
| app.UseSwaggerUI(c => | |
| { | |
| c.SwaggerEndpoint("/swagger/v1/swagger.json", "Mostrando API V1"); | |
| }); | |
| app.UseEndpoints(endpoints => | |
| { | |
| endpoints.MapControllers(); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment