Bonjour,

Je ne sais pas trop où poser ma question parce qu'elle est moitié C# et moitié WEB. Désolé par avance si ce n'est pas la bonne place.

J'utilise HttpWebRequest pour envoyer une requête POST vers un script php sur un serveur web. Ma requête a plusieurs paramètres et un fichier attaché. Le code fonctionne et le fichier arrive bien sur le serveur avec un nom temporaire et une taille. J'ai récupéré le contenu du fichier dans le répertoire temporaire et il correspond bien au fichier envoyé.

Mais le passage du nom du fichier ne se fait pas correctement. Si j'envoie dans la requête le bloc du fichier avec l'en-tête
Content-Disposition: attachment; name="file"; filename="D:\Documentations\regex php.pdf"
Content-Type: application/octet-stream
Le tableau $_FILES reçu dans php contient:

array
'file' =>
array
'name' => string 'octet-stream' (length=12)
'type' => string '' (length=0)
'tmp_name' => string 'D:\Temporaire\php856.tmp' (length=24)
'error' => int 0
'size' => int 133040

Comment faudrait-il modifier Content-Disposition pour avoir un résultat correct à l'arrivée ?

Voici le code complet :

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
 
public static void UploadFilesToRemoteUrl(string url, string[] files, NameValueCollection nvc)
{
 
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
 
    HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
    boundary;
    httpWebRequest2.Method = "POST";
    httpWebRequest2.KeepAlive = true;
    httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
 
    Stream memStream = new System.IO.MemoryStream();
 
    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
 
    string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
 
    foreach (string key in nvc.Keys)
    {
        string formitem = string.Format(formdataTemplate, key, nvc[key]);
        byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
        memStream.Write(formitembytes, 0, formitembytes.Length);
    }
 
    memStream.Write(boundarybytes, 0, boundarybytes.Length);
 
    string headerTemplate = "Content-Disposition: attachment; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
 
    for (int i = 0; i < files.Length; i++)
    {
 
        string header = string.Format(headerTemplate, "file", files[i]);
 
        byte[] headerbytes = System.Text.Encoding.ASCII.GetBytes(header);
        memStream.Write(headerbytes, 0, headerbytes.Length);
 
        FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) 
        {
            memStream.Write(buffer, 0, bytesRead);
        }
        memStream.Write(boundarybytes, 0, boundarybytes.Length);
 
        fileStream.Close();
    }
 
    sw.Close();
 
    httpWebRequest2.ContentLength = memStream.Length;
    Stream requestStream = httpWebRequest2.GetRequestStream();
 
    memStream.Position = 0;
    byte[] tempBuffer = new byte[memStream.Length];
    memStream.Read(tempBuffer, 0, tempBuffer.Length);
    memStream.Close();
    requestStream.Write(tempBuffer, 0, tempBuffer.Length);
    requestStream.Close();
 
    WebResponse webResponse2 = httpWebRequest2.GetResponse();
 
    Stream stream2 = webResponse2.GetResponseStream();
    StreamReader reader2 = new StreamReader(stream2);
 
    if (File.Exists(@"C:\reponse.html")) { File.Delete(@"C:\reponse.html"); };
    using (StreamWriter swi = new StreamWriter(@"C:\reponse.html", true))
    {
        swi.WriteLine(reader2.ReadToEnd());
    }
 
    webResponse2.Close();
    httpWebRequest2 = null;
    webResponse2 = null;
}
Papy !