Rename / Save image passed from Ajax to C #

0

I have the following code which shows an image in a thumbnail using jQuery, which my idea is to pass "the image" of ajax to the controller to do what I need

$('#subirFoto').on("change", function (event) {
    $("#arrastreImagen").html("");
    var salida = $("#subirFoto").val();
    alert($("#subirFoto")[0].files[0]);
    salida = URL.createObjectURL(event.target.files[0]);
    $("#arrastreImagen").append('<span><i class="fa fa-times btn btn-danger eliminarFoto"></i></span><div id="imagenBeer"><img src="' + salida + '" class="img-thumbnail"></div>')
    $.ajax({
        url: "guardarImagen",
        method: "POST",
        data: { img: salida },
        async: false,
        //dataType: "json",
        success: function (respuesta3) {


        }
    });
});

I know how to capture the data in the controller but how do I receive the image (I do not know if the data I'm passing is correct), rename it and save it in the "X" project folder? Thanks

    
asked by Baker1562 26.04.2018 в 19:43
source

1 answer

1

I understand that what you need is to receive a multipart

                var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());

                // aca obtenes los datos del formulario
                document = JsonConvert.DeserializeObject<DocumentVersionDTO>(provider.FormData["formData"]);
                // aca obtenes los files
                foreach (var file in provider.Files)
                {
                    // esto devuelve un byte[] con eso podes hacer lo que quieras, ponerlo en la base de datos o crear un file en un disco.
                    var archivo = file.ReadAsByteArrayAsync();
                }
    
answered by 26.04.2018 / 22:12
source