how can I enter the value of an input type="file" into another type of hidden input

0

How can I insert the value of an input type="file" into another type of hidden input. Please help I know it's a baby question anyway please help guys. I want it done in javascript or jQuery or php

    
asked by anonymous 21.03.2015 / 10:37

1 answer

1

You can grab the contents of a file using a FileReader , transforming it into a base64 string and put it in the hidden input

var fileUpload  = document.getElementById('fileUpload'),
    hiddenField = document.getElementById('hiddenField');

function fileChanged() {
    function onLoad(e) {
        // Adicionando o arquivo em base64 ao hidden field:
        hiddenField.innerHTML = e.target.result;
    }

    if (this.files && this.files[0]) {
        var fileReader = new FileReader();
        fileReader.onload = onLoad;
        // Isso vai transformar o arquivo em uma string base64:
        fileReader.readAsDataURL(this.files[0]);
    }
}

document.getElementById('#fileUpload')
    .addEventListener('change', fileChanged, false);
    
21.03.2015 / 19:17