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 :

Problème de pièce jointe


Sujet :

ASP.NET

  1. #1
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut Problème de pièce jointe
    Bonjour;
    Je cherche à ajouter une fonctionnalité attacher pièce jointe à mon formulaire asp.net sans succès.
    J'ai le message d'erreur suivant:
    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
     Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    Line 55:         
    Line 56:         String StrFileName = "";
    Line 57:         StrFileName = Request.Files["fileAttachment"].FileName;
    Line 58:         if (StrFileName != "") 
    Line 59:         {
    
    
    Source File: c:\Documents and Settings\TBSoltane\Mes documents\Visual Studio 2008\WebSites\Prestige\Fra\frContact.aspx.cs    Line: 57
    Le code frContact.aspx
    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
    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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    <%@ Page Language="C#" MasterPageFile="~/Fra/frMasterPage.master" AutoEventWireup="true" CodeFile="frContact.aspx.cs" Inherits="frContact" Title="Prestige Concepts Inc: Contact" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
    
        <div id="content-container-three-column">
            <!--
    
      CONTENT SIDE 1 COLUMN
    
      -->
            <div id="content-side1-three-column">
                <ul class="list-of-links">
                    <li class="current"><a href="frContact.aspx">Contact</a></li>
                    <li><a href="frDefault.aspx">Accueil</a></li>
                    <li><a href="frProducts.aspx">Produits</a></li>
                      <li><a href="frPartners.aspx">Partenaires</a></li>
                      <li><a href="frReferences.aspx">Réferences</a></li>
                    <li><a href="frAbout.aspx">A propos</a></li>
                    <li><a href="frTestimonials.aspx">Témoignages</a></li>
                </ul>
            </div>
            <!--
    
      CENTER COLUMN
    
      -->
            <div id="content-main-three-column">
                <h2><span id="heading">Contactez-nous</span></h2>
                <hr />
                <h3>
                    Siège social
                </h3>
                <p>
                    Prestige concepts<br />
                    LL1/9-Kortoba Riadh El Andalous<br />
                    2058 Ariana Tunis Tunisie
                </p>
                <p>
                    tel: (+216)71 82 07 47 / (+216)71 82 07 46
                    
                    <br />
                    fax: (+216)71 82 07 48
                </p>
                <a href="mailto:info@prestigeconcepts.com">info@prestigeconcepts.com</a><br />
               
                <p>
                </p>
                <br />
      
                <asp:Panel ID="Panel1" runat="server" Height="526px" BorderStyle="Dotted" BorderColor="gray"
                    Width="350px" >
                    <table>
                        <caption>
                            <br />
                            <tr>
                                <td style="width: 100px">
                                    Entreprise</td>
                                <td style="width: 100px">
                                    <asp:TextBox ID="txtCompany" runat="server" Width="175px"></asp:TextBox>
                                </td>
                                <td style="width: 100px">
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                                        ControlToValidate="txtCompany" 
                                        ErrorMessage="Prière de communiquer le nom de votre entreprise">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                    Nom</td>
                                <td style="width: 100px">
                                    <asp:TextBox ID="txtName" runat="server" Width="175px"></asp:TextBox>
                                </td>
                                <td style="width: 100px">
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                                        ControlToValidate="txtName" ErrorMessage="Prière de rentrer votre nom">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                    Email</td>
                                <td style="width: 100px">
                                    <asp:TextBox ID="txtEmail" runat="server" Width="175px"></asp:TextBox>
                                </td>
                                <td style="width: 100px">
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                                        ControlToValidate="txtEmail" 
                                        ErrorMessage="Prière de fournir votre adrese e-mail">*</asp:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                                        ControlToValidate="txtEmail" 
                                        ErrorMessage="Prière de fournir une adresse e-mail valide (xxx@yyy.zzz)" 
                                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px">
                                    Tél.
                                </td>
                                <td style="width: 100px">
                                    <asp:TextBox ID="txtPhone" runat="server" Width="175px"></asp:TextBox>
                                </td>
                                <td style="width: 100px">
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                                        ControlToValidate="txtPhone" 
                                        ErrorMessage="Prière de fournir votre numéro de téléphone">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 116px;">
                                    Message<span style="font-size: 10pt">*</span>
                                    <br />
                                </td>
                                <td style="width: 100px; height: 116px;">
                                    <asp:TextBox ID="txtMessage" runat="server" Height="106px" TextMode="MultiLine"></asp:TextBox>
                                </td>
                                <td style="width: 100px; height: 116px;">
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                                        ControlToValidate="txtMessage" ErrorMessage="Prière de saisir un message">*</asp:RequiredFieldValidator>
                                    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                                        ControlToValidate="txtMessage" 
                                        ErrorMessage="Pas plus de 1000 caractères, ni moins de 20" 
                                        OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
                                </td>
                            </tr>
                            <tr>
    					<td>
    						Attachment
    					</td>
    					<td>
    						<input type="file" runat="server" id="fileAttachment" name="fileAttachment" />
    					</td>
    				</tr>
                        </caption>
                    </table>
                   
                   <hr />
                   <asp:Button ID="btnValidate" runat="server" Text="Valider" Width="61px" 
                        style="left: 156px; position: relative; top: 10px" 
                        OnClick="btnValidate_Click" />&nbsp;<br />
                    <br />
                    <br />
                    <span style="font-size: 7pt">Tous les champs sont obligatoires
                        <br />
                        * = Maximum 1000 caractères, minimum 20.<br />
                        <br />
                        <asp:Label ID="lblError" runat="server"></asp:Label>
                    <br />
                    <br />
                    <br />
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="56px" 
                        Width="342px" />
                    </span></asp:Panel>
                &nbsp;
            </div>
            <!-- END MAIN COLUMN -->
            <!--
    
      CONTENT SIDE 2 COLUMN
    
      -->
            <div id="content-side2-three-column">
                <span id="subheading">Prestige Concepts à votre écoute.</span>
                <p>
                <br />
                <br />
                    Si vous avez une quetion, n'hésitez pas à nous contacter. Une équipe réactive et dynamique vous répondra rapidement.&nbsp;</div>
            <div class="clear">
                
                </div>
                
        </div>
    </asp:Content>
    Le code de frContact.aspx.cs
    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
    using System;
    using System.Web.Mail;
    using System.IO;
      
    public partial class frContact : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputFile file;
        protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
        {
            if ((txtMessage.Text.Length < 20) || (txtMessage.Text.Length > 1000))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtName.Text = "";
                txtEmail.Text = "";
                txtCompany.Text = "";
                txtPhone.Text = "";
                txtMessage.Text = "";
            }
        }
    
        protected void btnValidate_Click(object sender, EventArgs e)
        {
           
            MailMessage email = new MailMessage();
            
            email.From = "site@prestigeconcepts.fr";
            email.To = "info@prestigeconcepts.fr";
       
            email.Body =
                "Name :"
                + txtName
                + "\r\n"
                + "Email :"
                + txtEmail 
                + "\r\n"
                + "Company: "
                + txtCompany.Text
                + "\r\n"
                + "Phone : "
                + txtPhone.Text
                + "\r\n"
                + "Message : "
                + txtMessage.Text;
            
            String StrFileName = "";
            StrFileName = Request.Files["fileAttachment"].FileName;
            if (StrFileName != "") 
            {
                 
                //extraction fichier avec chemin
                StrFileName = StrFileName.Substring(StrFileName.LastIndexOf("\\") + 1);
                //enregistrement en temp
                Request.Files["fileAttachment"].SaveAs(Server.MapPath(StrFileName));
                //attacher le fichier 
                MailAttachment MyAttachment = new MailAttachment(Server.MapPath(StrFileName));
                email.Attachments.Add(MyAttachment);
            }
            email.Priority = MailPriority.High;
    
            SmtpMail.SmtpServer = "smtp.planet.tn";
    
            try
            {
                SmtpMail.Send(email);
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
            finally
            {
                lblError.Text = "Message envoyé avec succès";
                
            }
            if (StrFileName != "")
                    File.Delete(Server.MapPath(StrFileName));
    
        }
    
    }
    Je pense que je doit créer un objet et l'instancier pour résoudre le problème. Mais, j'ai beau chercher, je ne sais pas comment faire.

    Merci de votre aide.

  2. #2
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    StrFileName = fileAttachment.FileName;

  3. #3
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut
    Salut;
    J'ai essayé plusieurs façons de remplacer/rajouter par le code que tu as fourni, j'ai le message d'erreur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
    
    Compiler Error Message: CS0117: 'System.Web.UI.HtmlControls.HtmlInputFile' does not contain a definition for 'FileName'
    
    Source Error:
    
    Line 59: 
    Line 60: 
    Line 61:         StrFileName = fileAttachment.FileName;
    Line 62:        
    Line 63:         if (StrFileName != "")

  4. #4
    Membre Expert
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Par défaut
    Le plus simple est tout simplement de looper sur la Request.Files :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    for(int i = 0 ; i < Request.Files.Count ; i++) {
       Request.Files[i].SaveAs(... + Request.Files[i].FileName);
    }
    Ne pas oublier bien sûr multipart/form-data dans le form enctype bien sûr.

  5. #5
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut
    Le problème est au niveau de:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     StrFileName = Request.Files["fileAttachment"].FileName;
    Le débuggeur indique qu'il faut instancier l'objet en indiquant la ligne ci-dessus.

  6. #6
    Membre Expert
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    string StrFileName = Request.Files["fileAttachment"].FileName;
    ?

  7. #7
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    Line 57:     
    Line 58:         //String StrFileName = "";
    Line 59:         string StrFileName = Request.Files["fileAttachment"].FileName;
    Line 60:         //StrFileName = fileAttachment.FileName;
    Line 61:         if (StrFileName != "")
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
     
    Compiler Error Message: CS0128: A local variable named 'StrFileName' is already defined in this scope
     
    Source Error:
     
    Line 57:     
    Line 58:         String StrFileName = "";
    Line 59:         string StrFileName = Request.Files["fileAttachment"].FileName;
    Line 60:         //StrFileName = fileAttachment.FileName;
    Line 61:         if (StrFileName != "")

  8. #8
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Ah ok, t'ulises un control html, utilise le control asp:FileUpload
    C'est qd meme plus simple

  9. #9
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut
    Même avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
     <tr>
    <td>
    Attachment
    </td>
    <td>
    <asp:FileUpload  type="file" runat="server" id="fileAttachment" name="fileAttachment"  />
    </td>
    </tr>
    j'ai tjs le même problème
    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
     Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    Line 57:     
    Line 58:         String StrFileName = "";
    Line 59:         StrFileName = Request.Files["fileAttachment"].FileName;
    Line 60:         //StrFileName = fileAttachment.FileName;
    Line 61:         if (StrFileName != "") 
    
    
    Source File: c:\Documents and Settings\TBSoltane\Mes documents\Visual Studio 2008\WebSites\Prestige\Fra\frContact.aspx.cs    Line: 59

  10. #10
    Rédacteur
    Avatar de lutecefalco
    Profil pro
    zadzdzddzdzd
    Inscrit en
    Juillet 2005
    Messages
    5 052
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : zadzdzddzdzd

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 052
    Par défaut
    Utilise le code que j'ai mis au début

  11. #11
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut
    Merci à tous, surtout falco.

    Voici le code qui marche:

    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
     
     String StrFileName = "";
            StrFileName = fileAttachment.FileName;
     
            //StrFileName = Request.Files["fileAttachment"].FileName;
            //String savePath = @"c:\temp\uploads\";
     
            if (fileAttachment.HasFile) 
            {
     
                //extraction fichier avec chemin
                StrFileName = StrFileName.Substring(StrFileName.LastIndexOf("\\") + 1);
                //StrFileName = fileAttachment.FileName;
                //enregistrement en temp
                //savePath += fileName;
     
                //Request.Files["fileAttachment"].SaveAs(Server.MapPath(StrFileName));
                fileAttachment.SaveAs(Server.MapPath(StrFileName));
                //attacher le fichier 
                //MailAttachment MyAttachment = new MailAttachment(Server.MapPath(StrFileName));
                //fileAttachment.SaveAs(savePath);
     
                MailAttachment MyAttachment = new MailAttachment(Server.MapPath(StrFileName));
                email.Attachments.Add(MyAttachment);
            }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [OL-2003] Problème ouverture pièce jointe
    Par Barsy dans le forum Outlook
    Réponses: 0
    Dernier message: 03/09/2009, 09h43
  2. Problème lecture pièce jointe dans outlook phpmailer
    Par benjoux dans le forum Outlook Express / Windows Mail
    Réponses: 1
    Dernier message: 08/12/2008, 14h23
  3. Problème de pièce jointes
    Par L1011 dans le forum Outlook
    Réponses: 1
    Dernier message: 23/10/2008, 21h10
  4. Problème avec pièce jointe .jpg
    Par javelot69 dans le forum Outlook Express / Windows Mail
    Réponses: 14
    Dernier message: 25/10/2007, 13h55
  5. [Mail] Problème avec pièce jointe
    Par coco38 dans le forum Langage
    Réponses: 4
    Dernier message: 27/04/2007, 10h14

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