I have a script that’s broken and its currently trying to upload form contents to imgbb. I dont want that, instead i want it to save the form contents to a file locally on the webserver. How do i do this? Here’s the current code:
const formData = new FormData();
formData.append("image", canvas.toDataURL().split(',')[1])
var req = new XMLHttpRequest()
req.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
response = JSON.parse(this.response)
console.log(response)
url = response.data.image.url
$('#Loading').hide();
$('#URL').val(url).fadeIn();
}
}
req.open("POST", 'https://api.imgbb.com/1/upload?key=xxxxxxxxxxxxxxxxxxxxxxxxxx', true)
req.send(formData)
},
Ive tried the tutorial at https://www.tutorialspoint.com/how-to-create-and-save-text-file-in-javascript but it doesnt work.
2
Answers
Here you are sending a POST request with the image to the API you mentioned in the question. If you want to save it inside your web server directory, you can modify the code like this.
Client Side Code
Server Side Code
To save the form contents to a file locally on the webserver instead of uploading them to imgbb, you can modify the script as follows:
This script uses the
fs
module to write the form contents to a file namedformContents.txt
on the webserver. ThewriteFile
method takes three arguments: the name of the file, the data to be written, and a callback function that is called when the operation is complete. In this case, the callback function logs a message to the console indicating that the file has been saved.