Eduard Climov Eduard Climov - 2 months ago 4
Ajax Question

Save input file contents to a variable

Is it possible to set a variable for an input file element value, so it could further be assigned to another input?

This is, how it works with text inputs:

var tempValue = document.getElementById('text').value;


Basically, this is a popular question, so you can find it at these links (1, 2, 3).

But no, it is impossible – value property of input:file is read-only for security reasons – you can do it only on server-side. To read contents of the file, you have to use FileReader and specify how to read content (as plain text, as binary, or as a Blob) – example of reading as a Blob.