简体   繁体   中英

File API, web workers, and Chrome/Chromium

The following minimal HTML file results in an error in the browser's console. File is undefined when accessed from a web worker in Chrome.

I am somewhat puzzled by this: it is working perfectly well with Firefox and I'd expect Chrome to have this already ironed out, at in a development version (the problem seems present in Chrome 22, 23, and 24).

Am I missing something, or is there a workaround to get it to work with Chrome (or may be even other browsers) ?

<html>
<body>
<script type="text/javascript">
// File seems to be defined
var slice = File.prototype.webkitSlice;

window.URL = window.URL || window.webkitURL;
// File is not defined when creating the worker below
var blob = new Blob(["var slice = File.prototype.webkitSlice;"]);
var blobURL = window.URL.createObjectURL(blob);

// Getting:
// Uncaught TypeError: Cannot read property 'prototype' of undefined 
var worker = new Worker(blobURL);
</script>
</body>
</html>

If you change File to Blob , your script works. File inherits from Blob .

var blob = new Blob(["var slice = Blob.prototype.webkitSlice;"]);

For anyone following: crbug.com/147503

WebkitSlice is deprecated in chrome latest version so use slice instead of it. and use it

as ebidel stated

var blob = new Blob(["var slice = Blob.prototype.slice;"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM