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

Asked

Viewed 1,195 times

0

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

  • Entry forms file and kind multipart/form-data send all data (file parameters and content) not "Encoded URL" but in the body (body) of the requisition (fountain). This is done automatically for the contents of the file. If you want to have more control over the process (for example taking the contents of the file, encoding it in some way and using it as hidden field value) the only way I know is through the HTML5 Apis for File Manipulation.

  • Can you explain better what you want to do? Maybe I can use another logic to solve your problem?

1 answer

1


You can pick up the contents of a file using a Filereader, turn it into a Base64 string and put it into 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.