javascript createObjectURL 在 Chrome 中返回 undefined

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6755401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:44:11  来源:igfitidea点击:

createObjectURL is returning undefined in Chrome

javascriptfilevideoupload

提问by Rupsa

Trying to change video source file using createObjectDataURL. It is working fine with Firefox but not working in Chrome (version 12.0.742.122 m). code is not throwing any error but returns undefined for createObjectDataURL. I tried all the possibilities but it always returns undefined.

尝试使用 createObjectDataURL 更改视频源文件。它在 Firefox 中运行良好,但在 Chrome(版本 12.0.742.122 m)中不起作用。代码没有抛出任何错误,而是为 createObjectDataURL 返回 undefined。我尝试了所有的可能性,但它总是返回未定义。

<!DOCTYPE html>
<html>
<head>
<title>Check CreateObjectURL</title>
</head>
<script type="text/javascript" language="javascript">

window.URL = window.URL || window.webkitURL;

function ChangeProperty()
{
        var v = document.getElementById("myvideo");
        var file = document.getElementById("fileControl").files[0];
        v.setAttribute("src",window.URL.createObjectURL(file));
}
</script>
<body>
<div >
    <video id="myvideo" src="movie.ogg" controls ></video>
    <input type="file" id="fileControl" /> 
    <button id="btnprops" onClick="ChangeProperty()" >update</button>
</div>
</body>
</html>

Please help me. It has already taken lot of time. Thanks in Advance.

请帮我。这已经花费了很多时间。提前致谢。

回答by Martin Cohen

I had the same problem, when I was opening the page as a local file in Chrome. I had to use Apache and open it through localhost.

当我在 Chrome 中将页面作为本地文件打开时,我遇到了同样的问题。我不得不使用 Apache 并通过本地主机打开它。

回答by jonemo

This seems to be a bug in Chromium, though the status of the bug is unclear to me: http://code.google.com/p/chromium/issues/detail?id=91136

这似乎是 Chromium 中的一个错误,尽管我不清楚该错误的状态:http: //code.google.com/p/chromium/issues/detail?id=91136

Crossposting my comment there:

在那里交叉发布我的评论:

I can confirm this issue on Windows Chrome 17.0.932.0 dev-m. An easy way to confirm [the bug] is this page: http://html5-demos.appspot.com/static/a.download.html. It works when used online. After saving to disk, the download fails.

我可以在 Windows Chrome 17.0.932.0 dev-m 上确认这个问题。确认 [错误] 的一种简单方法是此页面:http: //html5-demos.appspot.com/static/a.download.html。它在在线使用时有效。保存到磁盘后,下载失败。

Edit: Just found this similar bug report (FileReader API not working for local content): http://code.google.com/p/chromium/issues/detail?id=60889This is marked won't fixfor a reason that appears applicable to the bug discussed here.

编辑:刚刚发现这个类似的错误报告(FileReader API 不适用于本地内容):http: //code.google.com/p/chromium/issues/detail?id=60889这被标记won't fix的原因似乎适用于这里讨论的错误。

回答by NeverAgain

I think you should use window.webkitURL.createObjectURL() as noted in https://developer.mozilla.org/en/DOM/window.URL.createObjectURL

我认为您应该按照https://developer.mozilla.org/en/DOM/window.URL.createObjectURL 中所述使用 window.webkitURL.createObjectURL()

Note: This method is prefixed in Chrome and Webkit as window.webkitURL.createObjectURL().

注意:此方法在 Chrome 和 Webkit 中的前缀为 window.webkitURL.createObjectURL()。

回答by Toby Okeke

If you get stuck in this

如果你被困在这

  video.srcObject = mediaSource;

Use teh code above. Set the srcObject to your stream.

使用上面的代码。将 srcObject 设置为您的流。