javascript - event.target.result 是空的?

标签 javascript html5-audio filereader

由于某些原因,在下面的代码中,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 either onload or onerror.

我怀疑您遇到了错误情况。添加一个 onerror 回调并查看 reader.error 必须说的内容。您可能希望使用单独的 onerroronabortonload 回调而不是 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/

相关文章:

javascript - 如何在 Cordova 中压缩 SQL 指令?

javascript - HTML5 : Playing live Opus audio frames without browser plug-in

javascript - 我如何使用从 audioContext 创建的分析器来检测是否可以听到播放的声音?

javascript - 来自 promise 的 Angular $http.post 值

javascript - 在用 getter 转换后,在 Mongoose 中抑制转换为 Date

javascript - adsense 不在我的 php 页面上显示广告

javascript - 使用javascript获取相对URL

javascript - 游戏开发: restart the sound(<audio> element) before it ended playing

HTML5 离线存储。文件存储?目录和文件系统 API

java - 是否可以仅使用 FileReader 从文件中读取?