1

I have an input type file in a form that I would like to be immediately read as a text file and used to update the value of a text input:

HTML:

<input type=file id='uploadfile' name='uploadfile' onchange="uploadCoordinates(this.form)">

JS:

function uploadCoordinates(form) {
       var file=form.uploadfile.value;
       var reader=new FileReader();
       reader.readAsText(file.value);
       form.coordinates.value=reader.result; 
}

Howveer, I get the following error: uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMFileReader.readAsText]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: https://portal.nersc.gov/project/als/ShirleyXAS/ShirleyXAS.js :: uploadCoordinates :: line 142" data: no]

1

2 Answers 2

1

try this there is error you are trying to read value.value

Correct code is

JS: function uploadCoordinates(form) {
       var file=form.uploadfile.value;
       var reader=new FileReader();
       reader.readAsText(file);
       form.coordinates.value=reader.result; }
Sign up to request clarification or add additional context in comments.

Comments

0

A better way to do this by fixing the error is to further modify your code and @cons0ul's code.

var rPreviousText;
function uploadCoordinates(formVal) {
   var file = form.uploadfile;
   var reader = new FileReader();
   reader.readAsText(file.value);
   form.coordinates.value = reader.result;
   rPreviousText = reader.result;
   showReadersText(reader.result);
}
function showReadersText(val) {
   alert(val); //I recommend using something better than alert.
}

This is far more reliable than either codes and has a method of showing the reader text that is in their txt file. If you don't need the showReadersText(), just delete it and replace it with whatever code you need. Please not that FileReader is not browser-friendly in more than half of the major browsers. It works well in Chrome, Firefox, and IE (at least when I tested them).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.