IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

Bouton de téléchargement avec la boite de dialogue qui propose ouvrir ou executer


Sujet :

ASP.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mars 2007
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 32
    Par défaut Bouton de téléchargement avec la boite de dialogue qui propose ouvrir ou executer
    Salut à tous depuis quelques jours je galère pour trouver comment faire apparaitre la fameuse boite de dialogue qui propose soit d'exécuter, soit d'ouvrir un fichier que l'on veut télécharger...

    Pour l'instant l'évènement de mon bouton me permet uniquement de télécharger le fichier que je sélectionne (dans un GridView), vers c:/.

    voila mon code c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
        // 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;
            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); 
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect(@"PageErreur.Aspx?Id=Problème lors du téléchargement", true);
                    }
                }
            }
            if (list.Count > 0)
            {
                Session["ListeDocSalarie"] = list;
            }
            connexion.Close();
        }

    et voici mon code asp.net (j'sais pas si c'est très utile mais bon) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
                <td style="width: 3px">
                    <asp:Button ID="btTelecharger" runat="server" OnClick="btTelecharger_Click" Text="Télécharger" />
                </td>
    Merci à celui (ou celle) qui me sortira de là, et me mettra sur la voie...

  2. #2
    Membre éprouvé
    Inscrit en
    Décembre 2003
    Messages
    108
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Décembre 2003
    Messages : 108
    Par défaut
    tu essayer ca:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    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();

  3. #3
    Membre averti
    Inscrit en
    Mars 2007
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 32
    Par défaut
    Tout d'abord merci pour ta réponse aussi rapide...

    J'ai mis ce que tu m'a envoyé comme suit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
            }
        }
    Mais ça me gère une exception :
    reponse non disponible dans ce contexte
    lol
    Qu'est ce que ça veut dire? Ai-je mal placé le code?

  4. #4
    Membre éprouvé
    Inscrit en
    Décembre 2003
    Messages
    108
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Décembre 2003
    Messages : 108
    Par défaut
    je ne sais pas où est ton code si c'est dans un usercontrol ou une page, si ce n'est pas le cas vérifie que tu as bien accès à l'objet Page.Response ou HttpContext en ajoutant une référence à System.Web si besoin

  5. #5
    Membre averti
    Inscrit en
    Mars 2007
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 32
    Par défaut
    pfff... honnêtement là je vois pas :s

  6. #6
    Membre éprouvé
    Inscrit en
    Décembre 2003
    Messages
    108
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Décembre 2003
    Messages : 108
    Par défaut
    quand tu build ou que tu debug ton projet, il te met la ligne où se situe l'erreur, regarde ce qui s'y passe

Discussions similaires

  1. [AC-2003] boite de dialogue qui propose l'impression
    Par olivier777 dans le forum IHM
    Réponses: 7
    Dernier message: 19/11/2009, 07h59
  2. Pb avec une boite de dialogue
    Par jerem721 dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 28/09/2007, 20h13
  3. Réponses: 1
    Dernier message: 15/06/2007, 17h37
  4. Réponses: 1
    Dernier message: 15/06/2007, 17h35
  5. [VBA-E] Probleme avec la boite de dialogue
    Par SybVicious dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 29/08/2006, 14h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo