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

C# Discussion :

Envoi de fichier POST


Sujet :

C#

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Envoi de fichier POST
    Bonjour,
    Je veux envoyer un fichier à un web service (POST). Je récupère le fichier stocker sur SharePoint sous forme de Byte[] (je sais pas si ça peu aider mais je peux également récupérer le Stream du document), je ne trouve pas d'exemple pour l’envoie de fichier sans passer par un Filestream, avez vous une piste ? Je pense que ce n'est pas grand chose mais je suis débutant....

    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
     public static string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
            {
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
     
                HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
                wr.ContentType = "multipart/form-data; boundary=" + boundary;
                wr.Method = "POST";
                wr.KeepAlive = true;
                wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
                Stream rs = wr.GetRequestStream();
                string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                foreach (string key in nvc.Keys)
                {
                    rs.Write(boundarybytes, 0, boundarybytes.Length);
                    string formitem = string.Format(formdataTemplate, key, nvc[key]);
                    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                    rs.Write(formitembytes, 0, formitembytes.Length);
                }
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
                string header = string.Format(headerTemplate, paramName, file, contentType);
                byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
                rs.Write(headerbytes, 0, headerbytes.Length);
                FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    rs.Write(buffer, 0, bytesRead);
                }
                fileStream.Close();
                byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                rs.Write(trailer, 0, trailer.Length);
                rs.Close();
                WebResponse wresp = null;
                    wresp = wr.GetResponse();
                    Stream stream2 = wresp.GetResponseStream();
                    StreamReader reader2 = new StreamReader(stream2);
                   var result = reader2.ReadToEnd();
     
                        wresp.Close();
                        wresp = null;
     
                        return result;
     
            }

  2. #2
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    467
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2007
    Messages : 467
    Points : 647
    Points
    647
    Par défaut
    Il y a plus simple pour uploader un fichier ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    WebClient client = new WebClient();
    byte[] response = client.UploadFile(url, chemin\complet\vers\le\fichier);

  3. #3
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 749
    Points
    39 749
    Par défaut
    Citation Envoyé par Luc1an0 Voir le message
    Il y a plus simple pour uploader un fichier ...

    WebClient client = new WebClient();
    byte[] response = client.UploadFile(url, chemin\complet\vers\le\fichier);
    Il y a même une méthode UploadData qui prend directement un byte[]

Discussions similaires

  1. Réponses: 1
    Dernier message: 25/04/2012, 10h54
  2. Réponses: 2
    Dernier message: 07/02/2008, 10h28
  3. Envoi fichier poste distant
    Par BATiViR dans le forum Delphi
    Réponses: 9
    Dernier message: 25/04/2007, 15h58
  4. Envoi de fichier text en post
    Par sandokan66 dans le forum Langage
    Réponses: 3
    Dernier message: 26/01/2007, 08h57
  5. [HTTP] Envoi de fichiers par http post
    Par java_girl dans le forum Entrée/Sortie
    Réponses: 6
    Dernier message: 28/08/2006, 16h44

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