4

For some reason, in the following code, evt.target.result is empty. Why is that?

function drop(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    var file = evt.dataTransfer.files[0];

    handleFiles(file, evt.target);
}

function handleFiles(file, target) {
    loadSongAnimate();

    var reader = new FileReader();

    // init the reader event handlers
    reader.onloadend = handleReaderLoadEnd;

    // begin the read operation
    reader.readAsDataURL(file);
}

function handleReaderLoadEnd(evt) {
    alert('Passing this: ' + evt.target.result);
    document.getElementById('audioTagId').src = evt.target.result;
}
1
  • Thanks for the edit @Mortensen. I get the hint, and will follow it in future... :D Commented Jun 5, 2011 at 0:57

2 Answers 2

8

From the fine manual:

onloadend
Called when the read is completed, whether successful or not. This is called after either onload or onerror.

I suspect that you have an error condition. Add an onerror callback and have a look at what reader.error has to say. You might want to use separate onerror, onabort, and onload callbacks instead of onloadend:

onabort
Called when the read operation is aborted.

onerror
Called when an error occurs.

onload
Called when the read operation is successfully completed.

That might make it easier to handle the individual events.


In your comment you say that you're getting an "error 2", from the other fine manual:

Constant: SECURITY_ERR
Value: 2
Description: The file could not be accessed for security reasons.

So it looks like you getting a "permission denied" error.

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

4 Comments

OK, I am getting an error code "2". This seems to be a generic catch-all error code, so I don't know how I can really troubleshoot it... any ideas on where to look from here?
@Crossdiver: Looks like you're getting "permission denied" (essentially), I've added a little update to that effect (and added a link that I forgot before).
@Crossdiver Thanks for the helpful debugging info. For the record, I was repeatedly getting SECURITY_ERR because I was viewing my html locally over file:// and not over http. Lesson learned!
Yes, that was my problem in the end as well... just because I was viewing the file locally, I would get this Security Error #2. Good to know!!
2

I was editing and viewing the file over a local file:// protocol. When you are referencing a local file inside another local file, the blank headers in the referenced local file with throw security errors.

Lesson learned... always upload to a server for testing as well. Would have saved me hours of Googling, and lots of hair.

1 Comment

You should have accepted mu's answer here, not your own.

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.