Save a GoogleMaps image on server with ASP.NET MVC 4 and C #

1

I'm doing a site where I use Google Maps API, I generate coordinates and I search for a specific site, then I take a site capture with html2canvas to generate an image in <img> , my question is:

How can I take this image to the controller?

This is the div the image is in:

<div id="googlemapimage">
    <img id="googlemapbinary" width="640" height="480"/>
</div>

In my controller I am using functions to transform the image as I want, but entered by httppostfilebase , can I use the image of div in the controller? If so, how?

    
asked by Oscar Vicente Burgoa Yujra 25.01.2016 в 18:06
source

2 answers

2
  • We are generating the Canvas from the image you have in that div with the function getBase64. I did it in the most generic way, you'll have to give it to play with the variables to get what you want.

    <html>
    <head></head>
    <script>
    
    function getBase64Image() {
         var img = document.getElementById("LaImagenAprocesar");
         // creamos un canvas en blanco
         var canvas = document.createElement("canvas");
         canvas.width = img.width;
         canvas.height = img.height;
    
         // Copiamos el contenido de la imagen al canvas
         var ctx = canvas.getContext("2d");
         ctx.drawImage(img, 0, 0);
    
         // Obtenemos el data-URL con formato de la imagen
         // Firefox soporta PNG y JPEG. Ojo aqui, hay que revisar 
         // el img.src al formato original, ten cuidado si utilizas image/jpeg,
         // porque  esto le dara un re-encode a la imagen
    
         var dataURL = canvas.toDataURL("image/png");
    
         return alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
     }
    </script>
    <body>
    
        <div>
            <img id="LaImagenAprocesar" src="unaimagen.png" alt="">
    
            <button type="button" onclick="getBase64Image()">mostrar data-url</button>
    
        </div>
    </body>
    </html>
    
  • With that string, use an Ajax Request or a post to send it to the controller. That variable with the name "myCadenaEnBase64" must be obtained from the previous method and well, I present you to Ajax using jQuery, it would be like this:

    $.ajax({
        cache: false,
        async: true,
        type: "POST",
        url: "http://example.com/MiControlador/Create",
        data: {
            imageData: miCadenaEnBase64 
        },            
        success: function () {
            alert('Se ha guardado la imagen!');
        }
    });
    
  • Here the implementation in the controller

    [HttpPost]
    public ActionResult Create(string imageData)
    {
        string fileName = "NombreDelArchivo.png";
        string fileNameWitPath = Path.Combine(Server.MapPath("~/FolderToSave"), fileName);
    
        using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
        {
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                byte[] data = Convert.FromBase64String(imageData);
                bw.Write(data);
                bw.Close();
            }
            fs.Close();
        }
    
            return RedirectToAction("Index");
    
    }
    
  • By the way, check to have write permissions in this folder.

        
    answered by 28.01.2016 в 02:26
    0

    If you are applying this technique

    HTML5 Canvas Save Drawing as an Image

    If you use toDataURL() then you are assigning img an image whose encode is base64. You can validate this that I mention with this example

    HTML5 Canvas: toDataURL ()

    There you will see how the image is generated in base64 and it is assigned to the img, this is how you would take it from img to send it to the controller.

    Get image data in JavaScript?

    Basically you should put the base64 content in a variable in javascript, but remove the part that defines the type of image.

    Then in asp.net mvc you define an action with a parameter of type string that will receive the image in base64. To invoke the action use $.ajax , $.post and in the parameters send the image as string.

    In the controller you would use Convert.FromBase64String to get the byte array of the image.

        
    answered by 25.01.2016 в 18:47