Created
December 10, 2021 08:49
-
-
Save NaClYen/6b456dcd9e6142b9732d02896313c376 to your computer and use it in GitHub Desktop.
javascript upload string blob as file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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