C sharp - Subir fichero a servidor php desde C#

 
Vista:

Subir fichero a servidor php desde C#

Publicado por darkblack (1 intervención) el 06/05/2022 14:03:43
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
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder