Is there any way for a client to upload a file in an HTML form (e.g. .txt or .csv formats) to a JavaScript variable as a string using only JavaScript? If so, could you provide a simple example? I don't want to use any PHP.
Thanks in advance!
Is there any way for a client to upload a file in an HTML form (e.g. .txt or .csv formats) to a JavaScript variable as a string using only JavaScript? If so, could you provide a simple example? I don't want to use any PHP.
Thanks in advance!
Here is a quick and dirty example based on a form named "myform" that contains a file input named "myfile" :
Here's the associated HTML form:
and a jsfiddle to demo it.
This variation on yent's answer manages multiple uploads and uses jquery:
HTML:
script:
Worth noting:
The object is already busy reading
.i
value which ended the loop.This jsfiddle demo preserves upload order by laying out divs in the change handler.