Adding blob to input

I am trying to add a data blob to an input field generated through DOM.
But it is not populating as a file for submission.

var ainput = $(‘’);

var dt = new DataTransfer();
dt.items.add(new File([blob], name));

ainput.files = dt.files;

What am i missing??

I have no idea how to answer your question, but I’ll just say (for what it’s worth) that those curly quotes in the first line look very suspicious.

As far as I know it doesn’t work like that. File input boxes are pretty much operated by the OS and you can’t just stick data into them to then submit them.

Plus this would be a security issue and so the file input doesn’t allow you to load data into it. It is only meant for the user to select a file from their local file system.

You can however create a file object and put it through FormData…

const binaryData = new Uint8Array([/* binary data here */]);
const file = new File([binaryData], "filename.bin", { type: "application/octet-stream" });

const formData = new FormData();
formData.append("file", file);

fetch("/upload", { method: "POST", body: formData });

Maybe that will work for you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.