How can I send an image (or more) from C # to my REST API and save it in mysql?

0

The following is my code and it works to send data from C # to my REST API and this information is stored correctly in the database:
so I send the information from C #:

using (var WC = new WebClient())
            {
                WC.Headers[HttpRequestHeader.ContentType] = "application/json;charset=utf-8";
                WC.Encoding = Encoding.UTF8;
                var datajson = josnencode;
                var response = WC.UploadString("http://www.mipaginaweb.mx/RegClient",datajson);
                dynamic respuestatxt = JsonConvert.DeserializeObject(response);
                string msjok = respuestatxt.mensaje;
                MessageBox.Show(msjok,"Cliente Registrado",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }  

Where "josnencode" is my serialized data in json format.

Data received from C # in json format in my API

if($_GET['url'] == "RegClient")
        {
            $json_string = file_get_contents("php://input");
            $body = json_decode($json_string);
            $nombre=$body->nombre;
            $retorno = transaccion::InsertClient($nombre);
            if ($retorno) {
               echo json_encode(array('resultado' => 'OK','mensaje' => 'Cliente registrado exitosamente!'));
           } else {
                echo json_encode(array('resultado' => 'NONE','mensaje' => 'No se pudo registra el cliente'));

            }

transactios.php

public function InsertClient($nombre)
{
$query = $sql ->prepare("INSERT INTO MiTabla(Nombre) VALUES(?)");
        $query->bind_param('s',$nombre);
        $query->execute();
}  

Now how can I send an image (or several) following the same logic of what I already have? Taking into account that I can insert the database with what I show you ...

Thanks for your comments.

    
asked by Manny 31.10.2018 в 21:23
source

1 answer

1

Taking your question

  

How can I send an image (or several) following the same logic as what I already have? Taking into account that I can insert the database with what I show you ...

Here are several actions

  • Send image via WebClient ( you have to convert it to a byte array)
  • ... then we must also know how to convert an image to byte [] in c #
  • In the DB you should save in MySQL the correct column type, BLOB, medium blob, varbinary, etc.
  • Let's see then by parts

    Send image via WebClient

    As you already know WebClient Has several methods, among which we can find UploadData (and its corresponding Async UploadDataAsync ) It can be seen in the signature of the method that sends the parameter byte [] data

    public void UploadDataAsync (Uri address, string method, byte[] data);
    

    NOTE: There is also the method UploadFile that you would have to give him the path of the file (and there are always issues of permission, or if you have it on disk or in db, etc.) but it is also a shorter alternative depending on your case.

    By subject Convert image to byte []

    Can help you

    But it's basically

     public byte[] ImageToByteArray(System.Drawing.Image imageIn)
     {
        using (var ms = new MemoryStream())
        {
           imageIn.Save(ms,imageIn.RawFormat);
           return  ms.ToArray();
         }
     }
    

    In the DB you should save in MySQL in the column

    Here it would be nice to keep the DB in the appropriate format (I can tell you the options in MS SQL Server) here in MySQL it would be nice to know the version or if you need more help about it

    I think it can help you

    Let us know if you want to delve into a particular topic I hope it will help or guide you

        
    answered by 31.10.2018 в 23:34