Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Resumable Fetch Example</title>
	</head>
	<body>
		<h1>Resumable Fetch Example</h1>
		<div>
			<progress id="progress" max="1" value="0"></progress>
			<button id="action">Download</button>
			<span id="status"></span>
		</div>
        <p>You may need to reduce the network speed and disable cache in your browser's developer tool to see the progress.</p>
        <p>It may not working if the response is compressed (like <code>content-type: gzip</code>).</p>
		<img width="640" height="360" alt="Photo of Gray Cat Looking Up Against Black Background by Snapwire (CC0)" id="img" style="display: none">
	</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                // WARNING: DO NOT USE THE CODE IN PRODUCTION, AS IT DOESN'T COVER ALL THE REQUEST AND ERROR CASES
class ResumableFetch {
	constructor(input, init) {
		this.aborter = null;
		this.request = null;
		this.length = 0;
		this.total = null;
		this.done = false;
		this.contentType = null;
		this.chunks = [];
		// ignore called as `fetch(input: Request)` at this time
		this.input = input;
		this.init = init || {};
	}

	start() {
		if (this.done) {
			return Promise.reject(new Error('The chunks have been piped out'));
		}
		if (this.aborter && !this.aborter.signal.aborted) {
			return this.request;
		}

		this.aborter = new AbortController();
		const { headers = {} } = this.init;
		const init = {
			...this.init,
			headers: {
				...headers,
				...(this.length ? {
					Range: `bytes=${this.length}-`
				} : false)
			},
			signal: this.aborter.signal
		};

		this.request = fetch(this.input, init).then((res) => {
			this.contentType = res.headers.get('content-type');
			const total = res.headers.get('content-length');
			if (!this.length) {
				this.total = total;
			} else if (total === this.total) {
				// server may not support range request
				this.reset();
			}
			return res.body.getReader();
		}).then(reader => this.readChunks(reader)).then((chunks) => {
			const stream = new ReadableStream({
				start(controller) {
					const push = () => {
						const chunk = chunks.shift();
						if (!chunk) {
							controller.close();
							return;
						}
						controller.enqueue(chunk);
						push();
					};
					push();
				}
			});

			return new Response(stream, {
				headers: {
					'content-type': this.contentType,
					'content-length': this.total
				}
			});
		}).catch((err) => {
			this.abort();
			this.aborter = null;
			throw err;
		});

		return this.request;
	}

	readChunks(reader) {
		return reader.read().then(({ value, done }) => {
			if (done) {
				this.done = true;
				return this.chunks;
			}

			this.chunks.push(value);
			this.length += value.length;
			if (this.onprogress) {
				this.onprogress({
					lengthComputable: !!this.total,
					total: this.total,
					loaded: this.length
				});
			}
			return this.readChunks(reader);
		});
	}

	abort() {
		if (this.aborter) {
			this.aborter.abort();
		}
	}

	reset() {
		this.chunks = [];
		this.length = 0;
	}
}


const url = 'https://objectstorage.ap-tokyo-1.oraclecloud.com/p/NdUkEhIYzjlI_H_9RRUeDb5pSym8geoWJ0hRVNctCEY/n/nrzblovvku9x/b/bucket-ccloli/o/adorable-animal-cat-730896.jpg';
const progress = document.getElementById('progress');
const action = document.getElementById('action');
const img = document.getElementById('img');
const status = document.getElementById('status');
let isDownloading = false;

const request = new ResumableFetch(url);
request.onprogress = ({ total, loaded }) => {
    progress.value = loaded / total;
    status.textContent = `Downloading (${loaded}/${total})`;
};
action.addEventListener('click', () => {
    if (isDownloading) {
        isDownloading = false;
        request.abort();
        action.textContent = 'Resume';
        status.textContent = 'Paused';
    } else {
        isDownloading = true;
        request.start()
            .then(res => res.blob())
            .then((blob) => {
                action.style.display = 'none';
                const url = URL.createObjectURL(blob);
                img.src = url;
                img.style.display = 'block';
                status.textContent = 'Done';
            });
        action.textContent = 'Pause';
        status.textContent = 'Downloading';
    }
});
              
            
!
999px

Console