esta es la respuesta desde el otro foro
Creo que s fácil darse cuenta de que si se utiliza C# (o VB, neto) y el control .NET fileupload. usted puede definir tipos de archivos en la lista de arrays "allowedExtensions".
string upload_Image(FileUpload fileupload, string ImageSavedPath)
{
FileUpload fu = fileupload;
string imagepath = "";
if (fileupload.HasFile)
{
string filepath = Server.MapPath(ImageSavedPath);
String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
try
{
string s_newfilename = DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileExtension;
fu.PostedFile.SaveAs(filepath + s_newfilename);
imagepath = ImageSavedPath + s_newfilename;
}
catch (Exception ex)
{
Response.Write("File could not be uploaded.");
}
}
}
}
return imagepath;
}
siento decir que es una código patético arriba. no eficiente. s_newfilename = DateTime.Now.ToString(); :) – LojiSmith
no recorra las extensiones aceptables, simplemente si (allowedExtensions.contains (extension)) está bien – LojiSmith
@LojiSmith ¿Qué cree exactamente que tiene la función contains? Pasa por la iteración. no hay nada de malo con el código anterior, excepto por el hecho de que no están saliendo del circuito una vez que encuentran una coincidencia ... – StephenKelzer