0

I'm new to JavaScript, so forgive me if the answer to this question is an obvious one.

I'm trying to set up an import function for a text-editor web-application.

My code is as follows:

function dataImport() {
    confirm("Are you sure you want to import the selected file? This will overwrite any data that is currently saved in the application workplace.");
    var fileReader = new FileReader();
    window.localStorage.setItem("AppData", fileReader.readAsText(document.querySelector("#import-selector").value));
};

And it should be activated by:

<input id="import-selector" type="file" /><button id="import-button" onclick="dataImport();">Import File</button>

Instead of writing the contents of the file to the localStorage, however, it merely writes the word 'undefined'. I take it some kind of error has happened, but I'm not sure what it is.

Thanks in advance for any help or advice.

1
  • Google Chrome, the dev channel. Commented Nov 24, 2010 at 6:21

1 Answer 1

2

I've never heard of a lick event in HTML. Maybe it's something new to HTML5 ;)

Try changing your onlick="... trigger to onclick="...

Maybe that will work?


Now that that's fixed, try this example: Chrome FileReader

I tried it, and it works flawlessly. It outputs the file contents into the error console.

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

Woah, that was stupid of me. Now I'm finally getting an error in the console! I've corrected the question to suit.
Hmm. I've repurposed the script you mentioned but I get this error: "Uncaught TypeError: Cannot read property 'length' of undefined". I've put the customized script online here: jsfiddle.net/CwfSy
Nevermind. For some reason it didn't like me having the script execute onclick, but when I set the input tag to do it onchange it worked.

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.