disintegratorD
Denoβ€’3y agoβ€’
36 replies
disintegrator

Streaming large files with fetch and FormData

Hi, I'm trying to find a way to build multiplart/form-data requests in Deno that contain large files. Ideally, I'll like these requests and the underlying files to be streamed out instead of needing to read the whole file into memory and adding them as parts to the request. I can't seem to find a built-in API for acquiring a Blob-like file handle. I also tried using npm:fetch-blob/from.js but that isn't compatible with Deno. Compared to other platforms:
Node.js v20+: the fs.openAsBlob function in node:fs can return a web File backed by the file system
Bun: The Bun.file function also returns a web File backed by the file system


import { fileFrom } from "npm:fetch-blob/from.js";

async function run() {
  const file = await fileFrom("./sample.txt");
  const fd = new FormData();
  fd.append("file", file);

  const res = await fetch("https://httpbin.org/anything/upload", {
    method: "POST",
    body: fd,
  });

  console.log(await res.json());
}

run();


Any suggestions on how I can make this work in Deno?
Was this page helpful?