[ Foro de C# ]

subir fichero a servidor php desde aplicacion c#

06-May-2022 14:44
Invitado (dartblack)
0 Respuestas

Buenas.

estoy intentando desde una aplicación Winforms realizada en C# subir un fichero a un servidor php.

El código que estoy usando en C# es el siguiente

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
try
           {
               ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
               ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
               ServicePointManager.Expect100Continue = true;
               ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                      | SecurityProtocolType.Tls11
                      | SecurityProtocolType.Tls12
                      | SecurityProtocolType.Ssl3;

               //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
               using (WebClient client = new WebClient())
               {
                   //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;// Tls12;
                   client.Headers.Add("Accept-Version", "2.0");
                   string filePath = @"C:\PDF\a.jpg";
                   var serverPath = new Uri(@"https://miservidor/Downloads/upload.php");
                   client.UploadFile(serverPath, filePath);
               }

               Application.Exit();
           }
           catch (Exception err)
           {
               MessageBox.Show(err.Message);
           }

En el código estoy intentando subir el fichero a.jpg y apunto a upload.php que está subido al servidor php.

El contenido del fichero upload.php es la siguiente

1
2
3
4
5
6
7
8
<?php
$uploads_dir = './img'; //Directory to save the file that comes from client application.
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
   $tmp_name = $_FILES["file"]["tmp_name"];
   $name = $_FILES["file"]["name"];
   move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>

Soy algo novato así que tengo un par de preguntas.

1.- ¿Veis algo mal en el código?
2.- Al ejecutar la aplicación obtengo lo siguiente:
Error en el servidor remoto: (406) No aceptable

He buscado información al respecto pero no me aclaro y no consigo hacer que me funcione.

¿Alguna idea para solucionarlo?

Muchas gracias




(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)