xmaniaxzOriginal post
Rank 3: Container
Im trying to grab a 2GB file from my storage using the API and Fetch.
JavaScript
const response = await fetch(`/api/download?fileID=664dfbbf6b74a086ac89`); if (!response.ok) { console.error("Failed to download file"); setIsDownloading(false); return; }
const contentLength = response.headers.get("content-length"); const total = parseInt(contentLength, 10); let loaded = 0;
const reader = response.body.getReader(); const stream = new ReadableStream({ start(controller) { function push() { reader.read().then(({ done, value }) => { if (done) { controller.close(); setDownloadCompleted(true); setIsDownloading(false); return; } loaded += value.length; setProgress((loaded / total) * 100); controller.enqueue(value); push(); }); } push(); }, });
const newResponse = new Response(stream); const blob = await newResponse.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "world.rar"; // Set the filename here document.body.appendChild(a); a.click(); a.remove(); };Summary
The developer is experiencing slow download times when fetching a large file from storage using the provided API. They are seeking a way to limit the download rate in the app but increase the file speed in the browser's download manager. The code provided includes fetching the file and displaying download progress.
Potential solution: Consider implementing a solution that streams the file in smaller chunks to improve download performance.