在下面的代码中,看起来好像我可以流式传输本地文件而无需立即将其全部加载到内存中,因为对于较大的文件,我会得到多个块(请参见下面的console.log(chunk.length);
)
const fileInput = document.getElementById('file-input');
const startButton = document.getElementById('start-button');
fileInput.addEventListener('change',() => {
console.log(fileInput.files);
});
startButton.addEventListener('click',() => {
if (fileInput.files && fileInput.files.length) {
const fileURL = URL.createObjectURL(fileInput.files[0]);
fetch(fileURL,{
method: 'GET',cache: 'no-store'
}).then(response => {
response.body.pipeTo(
new WritableStream({
write: chunk => {
console.log(chunk.length);
},abort: error => {
console.error(error);
},close: () => {
URL.revokeObjectURL(fileURL);
}
})
);
});
}
});
<input type="file" id="file-input" />
<button id="start-button">start</button>
但是我想知道的是,const fileURL = URL.createObjectURL(fileInput.files[0]);
是否创建到本地文件的链接,然后通过ReadableStream从读取中读取该文件,还是将整个文件加载到浏览器的内存和我看到的块中是从浏览器内存到Javascript虚拟机内存的“人工”或流式传输?