1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
protected void BtnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
try
{
//point d'entrée général!!!
string repGlobal = ConfigurationManager.AppSettings["RepertoireDocuments"];
string myFolder = String.Empty;
string repDocums = String.Empty;
string repImages = String.Empty;
//string repVideos = String.Empty;
//est-ce que l'user a défini un répertoire physique??
if (!String.IsNullOrEmpty(repGlobal))
{
//répertoire dédié Documents!!
repDocums = System.IO.Path.Combine(repGlobal, "Documents\\");
//on le créé s'il n'existe pas!!
if (!System.IO.Directory.Exists(repDocums))
System.IO.Directory.CreateDirectory(repDocums);
//répertoire Images!!
repImages = System.IO.Path.Combine(repGlobal, "Images\\");
if (!System.IO.Directory.Exists(repImages))
System.IO.Directory.CreateDirectory(repImages);
}
else
{
// "~/Data/Documents/" : celui-ci existe tjrs!!
repDocums = HttpContext.Current.Server.MapPath("~/Data/Documents/");
repImages = HttpContext.Current.Server.MapPath("~/Data/Images/");
}
//nom de sauvegarde généré automatiquement !
string nomcourt = System.IO.Path.GetRandomFileName(); // Guid.NewGuid().ToString();
//extension du fichier d'origine !
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
//nom de sauvegarde !
string nomsvgde = System.IO.Path.ChangeExtension(nomcourt, ext);
//Nomphysique : nom system (50char)
NomphysTextBox.Text = nomsvgde;
//Nomdocument : nom donné au document, modifiable par l'user (512char)
NomdocTextBox.Text = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
//si c'est une image on sauvegarde dans le dossier dédié!!!
string typefic = FileUpload1.PostedFile.ContentType;
if (typefic.StartsWith("image") || typefic.StartsWith("video") || typefic.StartsWith("audio"))
{
myFolder = repImages;
}
else
{
myFolder = repDocums;
}
//on sauvegarde, on sauvegarde sur le serveur !
FileUpload1.SaveAs(myFolder + nomsvgde);
//info après sauvegarde !
LabelMessage.Text = "Nom du fichier: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType + "<br>" +
"Sauvegardé sous : " + nomsvgde;
}
catch (Exception ex)
{
NomphysTextBox.Text = "";
NomdocTextBox.Text = "";
LabelMessage.Text = "ERREUR: " + ex.Message.ToString();
}
else
{
LabelMessage.Text = "Vous devez sélectionner un fichier, absolument!";
}
} |
Partager