由于某些原因,在下面的代码中,evt.target.result
是空的。这是为什么?
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;
}
最佳答案
来自fine manual :
onloadend
Called when the read is completed, whether successful or not. This is called after eitheronload
oronerror
.
我怀疑您遇到了错误情况。添加一个 onerror
回调并查看 reader.error
必须说的内容。您可能希望使用单独的 onerror
、onabort
和 onload
回调而不是 onloadend
:
onabort
Called when the read operation is aborted.
onerror
Called when an error occurs.
onload
Called when the read operation is successfully completed.
这可能会更容易处理单个事件。
在您的评论中,您说您收到来自 other fine manual 的“错误 2” :
Constant:
SECURITY_ERR
Value: 2
Description: The file could not be accessed for security reasons.
看来您遇到了“权限被拒绝”错误。
关于javascript - event.target.result 是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6238604/