DenoDDeno
Powered by
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
npm:fetch-blob/from.js
but that isn't compatible with Deno. Compared to other platforms:
Node.js v20+: the
fs.openAsBlob
fs.openAsBlob
function in
node:fs
node:fs
can return a web
File
File
backed by the file system
Bun: The
Bun.file
Bun.file
function also returns a web
File
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();
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?
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Streaming FormData
EsenteEEsente / help
3y ago
Importing local files (streaming large JSON files)
PhatsoPPhatso / help
2y ago
upload files formdata
ZabiZZabi / help
4y ago
how to hash very large files
andykaisAandykais / help
4y ago