admin管理员组文章数量:1318573
Suppose I have a large file I generate client-side that I want to allow the user to save to their hard drive.
The usual method would be to create a Blob, and then create an object URL for it:
const blob = new Blob([chunks], {type: 'application/example'});
linkEl.href = URL.createObjectUrl(blob);
This works, but isn't very efficient as it can quickly exhaust all available memory, since the resulting file has to remain in memory.
A better method would enable streaming. Something like this:
const outputStream = new WritableStream();
linkEl.href = URL.createObjectUrl(outputStream);
while (let chunk = await getChunk()) {
outputStream.write();
}
outputStream.end();
Is there a direct way to do this today?
The only method I've seen for streaming like this is to use a Service Worker. Unfortunately, there are many contexts in which a Service Worker isn't available. Privacy modes may bypass all service workers. Hard-refreshing the page disables them. Opening browser tools can reset the service worker state. The worker can be killed off at any time, and attempts to keep it alive with messaging are not guaranteed to work. All of these hacks have been implemented in an excellent project here: .js But, at the end of the day, it's unreliable due to these browser constraints.
Does a proper API exist for streaming a "download" client-side without the use of a service worker?
Suppose I have a large file I generate client-side that I want to allow the user to save to their hard drive.
The usual method would be to create a Blob, and then create an object URL for it:
const blob = new Blob([chunks], {type: 'application/example'});
linkEl.href = URL.createObjectUrl(blob);
This works, but isn't very efficient as it can quickly exhaust all available memory, since the resulting file has to remain in memory.
A better method would enable streaming. Something like this:
const outputStream = new WritableStream();
linkEl.href = URL.createObjectUrl(outputStream);
while (let chunk = await getChunk()) {
outputStream.write();
}
outputStream.end();
Is there a direct way to do this today?
The only method I've seen for streaming like this is to use a Service Worker. Unfortunately, there are many contexts in which a Service Worker isn't available. Privacy modes may bypass all service workers. Hard-refreshing the page disables them. Opening browser tools can reset the service worker state. The worker can be killed off at any time, and attempts to keep it alive with messaging are not guaranteed to work. All of these hacks have been implemented in an excellent project here: https://github./jimmywarting/StreamSaver.js But, at the end of the day, it's unreliable due to these browser constraints.
Does a proper API exist for streaming a "download" client-side without the use of a service worker?
Share Improve this question asked Jul 13, 2020 at 20:18 BradBrad 164k55 gold badges377 silver badges553 bronze badges 1- Related (but different) question: stackoverflow./q/58014109/362536 – Brad Commented Jul 13, 2020 at 20:19
1 Answer
Reset to default 10There is one being defined... File System Access.
It's still an early draft and only Chrome has implementing it.
You would be particularly interested in the FileSystemWritableFileStream interface which will allow to write on disk after the user chooses where you can mess with their data ;-)
Non live code since "Sandboxed documents aren't allowed to show a file picker."...
onclick = async () => {
if( !("showSaveFilePicker" in self) ) {
throw new Error( "unsupported browser" );
}
const handle = await showSaveFilePicker();
const filestream = await handle.createWritable();
const writer = await filestream.getWriter();
// here we have a WritableStream, with direct access to the user's disk
await writer.write( "hello" );
await writer.write( " world" );
writer.close();
};
Here is a live glitch poject.
本文标签:
版权声明:本文标题:javascript - Streaming a client-side generated response as a download, without service worker - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048898a2417957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论