我尝试了所有可能的方法,在客户端使用回调,$ .when,deferred(但我需要多次运行)。数据显示在文本框中,但显示为警报为空。
self.addEventListener('message', function(e) {
var files = e.data;
var buffers = [];
// Read each file synchronously as an ArrayBuffer and
// stash it in a global array to return to the main app.
[].forEach.call(files, function(file) {
var reader = new FileReaderSync();
buffers.push({
data: reader.readAsArrayBuffer(file),
name: file.name
});
});
postMessage(buffers);
}, false);
<input id="filename" type="file" multiple="multiple" />
<script>
var worker = new Worker('js/worker.js');
worker.onmessage = function(e) {
document.getElementById('text1').value = e.data;
//console.log(e.data); // e.data should be an array of ArrayBuffers.
};
worker.onerror = function(e) {
document.querySelector('#error').textContent = [
'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message
].join('');
};
alert('Load');
document.getElementById('filename').addEventListener('change', function(e) {
worker.postMessage(this.files);
alert('Data Load' + document.getElementById('text1').value);
}, false);
</script>
<input type="text" id="text1" value="text" />