ASP.NET - Subir un archivo al Servidor

Written by lopezatienza on 27 Enero 2009 – 12:43 -

Para subir a un archivo a nuestro servidor, primeramente deberemos importar la librería System.IO a nuestra página:

  1. using System.IO;

Deberemos añadir un componente FileInput, en mi caso se llamará File1 que el nombre que se le da de manera predeterminada. He añadido también la opción de subirlo a una carpeta determinada, y si no existe, la creo:

  1. string Path = HttpContext.Current.Server.MapPath("");
  2. if (!System.IO.Directory.Exists(Path + @"\Nombre_Carpeta"))
  3. {
  4. System.IO.Directory.CreateDirectory(Path + @"\ Nombre_Carpeta");
  5. }
  6. string Archivo = string.Empty;
  7. Archivo = File1.PostedFile.FileName;
  8. Archivo = System.IO.Path.GetFileName(Archivo);
  9. File1.PostedFile.SaveAs(Path + @"\Nombre_Carpeta\" + Archivo);

Tags:
Posted in ASP.NET |

Leave a Comment

RSS