Node.js FormData and File references

66 Views Asked by At

Suppose I want to use the Fetch API to upload a local file as part of FormData with Node.js.

If this were a browser, I might have a file or blob reference already, such as from a file input element. In that case, I just append the file reference to the FormData instance, and the user agent takes care of streaming it from disk, like so:

const formData = new FormData();
formData.append('file', fileInputElement.files[0]);

const res = await fetch(postURL, {
  method: 'POST',
  body: formData
});

Now, in the Node.js case, there is no file input element. All I have is a path on disk. How can I get a Blob or File reference from that?

Some things I've looked into:

Is there any way to get a File reference that FormData can use, for a local file on disk, with Node.js?

0

There are 0 best solutions below