How to convert a file file to String base 64 with javascript in jsp?

-1

A help, I need to convert the file file (photo jpg) to String base64 and send it as a String through the json or my action reserve.

  

The jsp view where I declare the File

  

Json where I send the values entered to the reserve action

  

Action reserves who receives the String type variables.   Also how would the String 64 base get in the action to save it in my bd as a blob?

  

Console error:   Notification (set struts.devMode to false to disable this message):   Unexpected Exception caught setting 'fotosBytes' on' class com.redsocial.action.ProgramacionAction: Error setting expression 'fotosBytes' with value' [Ljava.lang.String; @ 3067000 '   ERROR [http-nio-8081-exec-2] - Could not find action or result   / DAWI_Canchitav_vEdu_v6_System / reserve? TextBooking = zzzz & startdate = 2017-11-13 + 15% 3A30% 3A00 & enddate = 2017-11-13 + 16% 3A00% 3A00 & dniClient = 123 & photosBytes = C% 3A% 5Cfakepath% 5C0 .jpg & promotion = Frame & idCancha = 1   No result defined for action com.redsocial.action.ProgramacionAction and result input

    
asked by Eduardo Cubas 20.11.2017 в 13:51
source

1 answer

0

I used the following function to convert the files or images to base 64

function SelectedFile() {
        //Toma el archivo elegido por el input 
        var value = document.getElementById("file").files[0];

        //Este objeto FileReader te permite leer archivos
        var reader = new FileReader();

        //Esta función se ejecuta cuando el reader.readAsDataURL termina 
        reader.onloadend = function (e) {
            NombreDeTuVariableAEnviar = e.target.result.split("base64,")[1];
        }

        //Aqui comienza a leer el archivo para posteriormente ejecutar la función onloadend
        reader.readAsDataURL(value);

}

Here you can check the documentation on how to use FileReader , I saved the database, I hope it helps you, Regards.

    
answered by 20.11.2017 / 16:10
source