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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
var nbrFichiers = 0;
function init()
{
//Création d'un premier input
creerInput();
}
//Fonction renvoyant le nom d'un fichier à partir de son chemin complet
function getFileName(fileName)
{
if (fileName != "") {
if (fileName.match(/^(\\\\|.:)/)) {
var temp = new Array();
temp = fileName.split("\\");
var len = temp.length;
fileName = temp[len-1];
} else {
temp = fileName.split("/");
var len = temp.length;
if(len>0)
fileName = temp[len-1];
}
}
return fileName;
}
function creerInput()
{
//Création de l'élément input
var input = document.createElement("input");
input.type = "file";
//Lorsqu'un fichier est choisi, on ajoute son nom à la liste
input.onchange = function() {
ajouterFichier(this);
}
//Ajout de l'input au document
$("input").appendChild(input);
}
function ajouterFichier(input)
{
if(nbrFichiers == 0)
$("fichiers").removeChild($("fichiers").firstChild);
//Création de la ligne dans la liste des fichiers à uploader
var fichier = document.createElement("p");
//Image de suppression
var image = document.createElement("img");
image.src = "images/suppr.gif";
image.alt = "supprimer";
Element.setStyle(image, {border: "0px", verticalAlign: "top"});
//Lien pour supprimer
var lnk = document.createElement("a");
lnk.href= "#";
lnk.onclick = function () {
supprimerFichier(fichier, input);
}
//Ajout de l'image dans la balise de lien
lnk.appendChild(image);
//Ajout du lien à la ligne de la liste
fichier.appendChild(lnk);
//Ajout du nom du fichier
fichier.appendChild(document.createTextNode(" " + getFileName(input.value)));
Element.setStyle(fichier, {margin: "0", padding: "0"});
//Ajout de l'item à la liste
$("fichiers").appendChild(fichier);
nbrFichiers++;
//Affectation de l'attribut name de l'input
input.name = getFileName(input.value);
new Effect.Highlight(fichier, {startcolor: "#7fd9ff", endcolor: "#FFFFFF"});
//Création d'un nouvel input pour un nouveau fichier
Element.hide(input);
creerInput();
}
function supprimerFichier(item, input)
{
//Suppression de l'item dans la liste des fichiers à uploader
new Effect.Fade(item, {afterFinish: function () { finSuppr(item); } });
//Suppression de l'input pour que le fichier supprimé ne soit pas envoyé par le formulaire
$("input").removeChild(input);
}
function finSuppr(item)
{
$("fichiers").removeChild(item);
nbrFichiers--;
if(nbrFichiers == 0)
$("fichiers").appendChild(document.createTextNode("Aucun fichier à uploader"));
} |