Mettre en oeuvre un "Ouvrir Avec"
Bonjour tout le monde.
Ca fait plus de 3 jours que je tourne en rond avec une fonctionnalité et je craque, donc et vous demande votre aide.
Dans l'application que nous développons, on doit proposer à l'utilisateur d'ouvrir ou de télécharger certains fichier (TXT, CSV, XLS ou XLSX) disponibles sur le serveur.
Je cherche donc à reproduire la boite "Ouvrir avec..." qui fait exactement ce qu'on veux.
On a donc une fonction qui remplie une table avec un bouton en dernière cellule de chaque ligne.
Une des cellules de la table contient le nom complet du fichier à récupérer ou à ouvrir et est dans la variable WFic.
(Cette variable est forcée à cause du confinement, car je n'ai pas accès au serveur).
Mais je n'arrive pas à implémenter la fonction d'ouverture.
J'ai bien pensé au download du html5, mais ça ne fonctionne pas.
J'espère que je suis clair dans mes explications et merci d'avance pour votre précieuse aide.
Voici la fonction brut
Code:
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
function VerificationFichier(P_Champs01)
{
writeFile(G_FICLOG, "VerificationFichier(P_Champs01)", "OUI");
let WNomFic = P_Champs01[28];
let WDateDeb = $('#date_deb').datepicker("getDate");
let WDateFin = $('#date_fin').datepicker("getDate");
let WNomFicATraiter = RecupFormat(WNomFic, WDateDeb); // Récupère le format de la date dans le nom du fichier et on remplace par WDateDeb
let WLigne = $('#ds_clair').html() + Remplace(WNomFicATraiter, "categorie", WChamps01[27]);
$('#verif').html(WLigne);
SelectParametre("NB_FICHIER_SRC_MAX");
let WNb_FichierSRC = parseInt(G_SELECT_PARAMETRE);
let f = 0;
let WNumLig = 1;
let WDossier = "";
let WDate = "";
let WNomComplet = "";
let WNomFic2 = "";
let WDos = "";
let WDos2 = "";
// Vidage de table_depend en laissant la 1ère ligne (on laisse les thead, mais on supprimer les tbody)
$("#table_depend tbody").empty();
while (f <= WNb_FichierSRC)
{
if (P_Champs01[f] != "") // META_FICHIER_SRC_n
{
let B_DateDeb = new Date(WDateDeb);
let B_DateFin = new Date(WDateFin);
while (B_DateDeb <= B_DateFin)
{
let WDateDeb2 = FormatDate2(B_DateDeb, "JJ/MM/AAAA")
let WPos = P_Champs01[f].indexOf("_backslash_");
let WPosFin = WPos + 11; // "_backslash_" -> 11 caractères
if (WPos != -1)
{
WDos = P_Champs01[f].substr(0, WPos);
if (WDos.indexOf("DOSSIER") != -1)
{
WNomFic2 = P_Champs01[f].substr(WPosFin, P_Champs01[f].length);
SelectParametre(WDos);
WDos2 = G_SELECT_PARAMETRE;
}
else
{
WNomFic2 = Remplace(P_Champs01[f], "_backslash_", "\\");
}
}
else
{
WDos = "";
WNomFic2 = P_Champs01[f];
};
WNomComplet = RecupFormat(WNomFic2, B_DateDeb); //WNomFic2;
let WFic = WDos2 + WNomComplet;
//WNomFicATraiter = "/volume1/web/CTRL_BDD_MULTIVAC_SG/LOG/2020_04_07_LOG_CTRL_BDD_MULTIVAC_SG.txt";
if (f == 2 || f == 4 || f == 6)
{
WFic = "/volume1/web/CTRL_BDD_MULTIVAC_SG/LOG/2020_04_10_LOG_CTRL_BDD_MULTIVAC_SG.txt";
}
let WLigTableDepend = document.createElement("tr");
WLigTableDepend.id = "DL" + twoDigit(WNumLig);
let WCell01 = document.createElement("td");
WCell01.id = "DL" + twoDigit(WNumLig) + "C01";
// WCell01.innerHTML = P_Champs01[f];
let PWCell01 = document.createElement("label");
PWCell01.innerHTML = P_Champs01[f];
WCell01.appendChild(PWCell01);
let WCell02 = document.createElement("td");
WCell02.id = "DL" + twoDigit(WNumLig) + "C02";
WCell02.innerHTML = WDateDeb2;
let WCell03 = document.createElement("td");
WCell03.id = "DL" + twoDigit(WNumLig) + "C03";
WCell03.innerHTML = WFic;
let WCell04 = document.createElement("td");
let BtnCell04 = document.createElement("button");
BtnCell04.title = "Ouverture du fichier : \r\n" + WFic;
BtnCell04.onclick = function()
{
OuvreFic(WFic);
};
WCell04.id = "DL" + twoDigit(WNumLig) + "C04";
// WCell04.appendChild(BtnCell04);
let Wa = document.createElement("a");
Wa.setAttribute("download_bis", WFic);
Wa.appendChild(BtnCell04);
WCell04.appendChild(Wa);
WLigTableDepend.appendChild(WCell01);
WLigTableDepend.appendChild(WCell02);
WLigTableDepend.appendChild(WCell03);
WLigTableDepend.appendChild(WCell04);
PWCell01.className = "wrapping"; // On applique la classe au <P> dans la cellule
WCell01.className = "CentrageVerticalForce";
WCell02.className = "CentrageVerticalForce";
WCell03.className = "CentrageVerticalForce";
BtnCell04.className = "btn btn-warning fas fa-folder-open fa-lg center";
WCell04.classList.add("text-center");
WCell04.classList.add("Table_Largeur_Col1");
WCell04.className = "CentrageVerticalForce";
document.getElementById("dependRows").append(WLigTableDepend);
document.getElementById("DL" + twoDigit(WNumLig)).onclick = function()
{
$("#table_caract tbody").empty();
tableControl("table_depend", WLigTableDepend.id, "FondBordeau", "LIG");
AffNiveau2(this.id, this.rowIndex, P_Champs01, WFic);
}
fileExists(WFic, "NON");
if (G_FILE_EXIST == "OK")
{
WCell01.classList.add("FondVert");
WCell02.classList.add("FondVert");
WCell03.classList.add("FondVert");
WCell04.classList.add("FondVert");
BtnCell04.disabled = false;
}
else
{
WCell01.classList.add("FondRouge");
WCell02.classList.add("FondRouge");
WCell03.classList.add("FondRouge");
WCell04.classList.add("FondRouge");
BtnCell04.disabled = true;
}
B_DateDeb = new Date(B_DateDeb.setDate(B_DateDeb.getDate()+1));
};
}
WNumLig = WNumLig + 1;
f = f + 1;
}
...
...
... |
Les essais en PHP :
Code:
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
|
function OuvreFic(P_Fichier)
{
writeFile(G_FICLOG, "OuvreFic(" + P_Fichier + ")", "OUI");
alert("Ouverture du fichier : " + P_Fichier);
// download_bis(P_Fichier, "il faut lire le fichier texte pour le charger");
$.ajax(
{
type: "POST",
url: "./PHP/Download.php",
data: {NomFichier : P_Fichier},
async: false,
error : function()
{
alert("Echec de l'envoi de la requête : " + P_Fichier);
},
success: function(data)
{
// console.log("envoyé avec succès");
}
});
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
<?php
$file = $_POST['NomFichier'];
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.'im.jpg'.'"'); //Nom du fichier
header('Content-Length: '.intval('82927')); // Taille du fichier
//readfile('/volume1/web/CTRL_BDD_MULTIVAC_SG/LOG/2020_04_10_LOG_CTRL_BDD_MULTIVAC_SG.txt');// nom (et emplacement) du fichier dans le serveur
readfile($file);// nom (et emplacement) du fichier dans le serveur
?> |
Je précise que j'essaye 2 trucs à la fois : une fonction "OuvreFic" au click sur le bouton d'un côté et un ajout du ciode HTML5 DONWLOAD.
Bref, j'espère que vous pourrez m'aider à me dépatouiller de ce truc et ces temps compliqués.