evt.target.result 为空?
由于某种原因,在以下代码中,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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自精细手册:
我怀疑您遇到了错误情况。添加
onerror
回调并查看reader.error
所说的内容。您可能需要使用单独的onerror
、onabort
和onload
回调,而不是onloadend
:这可能会让处理个别事件变得更容易。
在您的评论中,您说您收到来自 其他精美手册:
所以看起来您收到了“权限被拒绝”错误。
From the fine manual:
I suspect that you have an error condition. Add an
onerror
callback and have a look at whatreader.error
has to say. You might want to use separateonerror
,onabort
, andonload
callbacks instead ofonloadend
: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:
So it looks like you getting a "permission denied" error.
我正在通过本地 file:// 协议编辑和查看该文件。当您在另一个本地文件中引用本地文件时,引用的本地文件中的空白标头会引发安全错误。
吸取的教训...始终上传到服务器进行测试。本来可以节省我几个小时的谷歌搜索时间和很多头发。
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.