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
|
// Bouton de téléchargement
protected void btTelecharger_Click(object sender, EventArgs e)
{
// Lancement d'un thread qui se chargera du téléchargement
Thread thread = new Thread(new ThreadStart(Telecharger));
thread.Start();
}
// Méthode de téléchargement
public void Telecharger()
{
string reqSelectRacine, reqSelectDossierPrinc, reqSelectSousDossier, reqSelectIdSociete, reqSelectJourDate, reqSelectMoisDate, reqSelectAnneeDate;
string racine, dossierPrinc, sousDossier, typeDocument, matricule, idSociete, jour, mois, annee;
string dateDocument, chemin, nomFichier, message;
string ficGlobal = Server.MapPath("~\\temp\\" + Session.SessionID);
SqlConnection connexion = new SqlConnection("Data Source=STQY-589\\SQLEXPRESS; Initial Catalog=DossierIndividuel;Integrated Security=True");
List<string[]> list = new List<string[]>();
int nbLigne = gvSalarie.Rows.Count;
for (int i = 0; i < nbLigne; i++)
{
list.Add(new string[2]{gvSalarie.DataKeys[i]["matricule"].ToString(),
gvSalarie.DataKeys[i]["typeDocument"].ToString()});
if (((CheckBox)gvSalarie.Rows[i].FindControl("uneSelection")).Checked)
{
try
{
if (connexion.State == ConnectionState.Closed)
{
connexion.Open();
}
reqSelectRacine = "SELECT racine " +
"FROM CHEMIN, DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' " +
"AND CHEMIN.idChemin = DOCUMENT.idChemin ";
reqSelectDossierPrinc = "SELECT dossierPrincipal " +
"FROM CHEMIN, DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' " +
"AND CHEMIN.idChemin = DOCUMENT.idChemin ";
reqSelectSousDossier = "SELECT sousDossier " +
"FROM CHEMIN, DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' " +
"AND CHEMIN.idChemin = DOCUMENT.idChemin ";
reqSelectIdSociete = "SELECT idSociete " +
"FROM DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' ";
reqSelectJourDate = "SELECT DAY(dateDocument) " +
"FROM DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' ";
reqSelectMoisDate = "SELECT MONTH(dateDocument) " +
"FROM DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' ";
reqSelectAnneeDate = "SELECT YEAR(dateDocument) " +
"FROM DOCUMENT " +
"WHERE [matricule] = '" + gvSalarie.DataKeys[i]["matricule"].ToString() + "' " +
"AND [typeDocument] = '" + gvSalarie.DataKeys[i]["typeDocument"].ToString() + "' ";
// Exécution des requêtes
SqlCommand cmdSelectRacine = new SqlCommand(reqSelectRacine, connexion);
SqlCommand cmdSelectDossierPrinc = new SqlCommand(reqSelectDossierPrinc, connexion);
SqlCommand cmdSelectSousDossier = new SqlCommand(reqSelectSousDossier, connexion);
SqlCommand cmdSelectIdSociete = new SqlCommand(reqSelectIdSociete, connexion);
SqlCommand cmdSelectJourDate = new SqlCommand(reqSelectJourDate, connexion);
SqlCommand cmdSelectMoisDate = new SqlCommand(reqSelectMoisDate, connexion);
SqlCommand cmdSelectAnneeDate = new SqlCommand(reqSelectAnneeDate, connexion);
racine = cmdSelectRacine.ExecuteScalar().ToString();
dossierPrinc = cmdSelectDossierPrinc.ExecuteScalar().ToString();
sousDossier = cmdSelectSousDossier.ExecuteScalar().ToString();
typeDocument = gvSalarie.DataKeys[i]["typeDocument"].ToString();
matricule = gvSalarie.DataKeys[i]["matricule"].ToString();
idSociete = cmdSelectIdSociete.ExecuteScalar().ToString();
jour = cmdSelectJourDate.ExecuteScalar().ToString();
mois = cmdSelectMoisDate.ExecuteScalar().ToString();
annee = cmdSelectAnneeDate.ExecuteScalar().ToString();
dateDocument = jour + "-" + mois + "-" + annee;
chemin = racine + "" + dossierPrinc + "" + sousDossier + "" + typeDocument + "" + matricule + "" + idSociete + "" + dateDocument + ".pdf";
nomFichier = typeDocument + "" + matricule + "" + idSociete + "" + dateDocument + ".pdf";
/* // Création d'un objet WebClient (c'est lui qui permet de télécharger un fichier)
WebClient wc = new WebClient();
// Lancement du téléchargement
wc.DownloadFile(chemin, "c:/" + nomFichier);
// Destruction de l'objet WebClient
wc.Dispose();
// Et un petit message pour dire que le téléchargement est terminé
message = "Téléchargement effectué vers c:/";
Outil.Message(message, this); */
Page.Response.Clear();
Page.Response.ContentType = "application/pdf";
Page.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
Page.Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", nomFichier));
Page.Response.TransmitFile(nomFichier);
Page.Response.End();
}
catch (Exception ex)
{
Response.Redirect(@"PageErreur.Aspx?Id=Problème lors du téléchargement", true);
}
}
}
if (list.Count > 0)
{
Session["ListeDocSalarie"] = list;
}
} |
Partager