Skip to content

Instantly share code, notes, and snippets.

@NaClYen
Created December 10, 2021 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NaClYen/6b456dcd9e6142b9732d02896313c376 to your computer and use it in GitHub Desktop.
Save NaClYen/6b456dcd9e6142b9732d02896313c376 to your computer and use it in GitHub Desktop.
javascript upload string blob as file
function upload(txt) {
// wrapper by Blob
const blob = new Blob([txt], { type: "text/html" });
// warpper by File
const file = new File([blob], "test.plan");
const formData = new FormData();
formData.append("File", file);
fetch("http://localhost/upload", {
method: "POST",
body: formData,
})
.then(async (res) => {
const txt = await res.text();
if (res.ok) return txt;
else throw new Error(txt);
})
.then((success) => console.log(`upload success: ${success}`))
.catch((err) => console.error(`upload failed: ${err}`));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment