uploading image to wordpress site with javascript fetch function failed
-
I tried to turn an html div element into an png image than I want upload this image in my wordpress site . I wrote following codes, But it is showing image upload failed. Can You find where is wrong. Any help to correct this code appreciated.
// Capture the div element as a PNG image function convertDivToImage() { html2canvas(document.querySelector("#wgr")).then(canvas => { const imageData = canvas.toDataURL("image/png"); // Set the Content-Type header let headers = new Headers(); headers.append("Content-Type", "image/png"); // Set the authorization header const authHeader = "Basic " + btoa("username:password"); // Upload the image to the WordPress site using the REST API return fetch("/wp-json/wp/v2/media", { method: "POST", headers: { "Content-Type": "image/png", "Authorization": authHeader}, body: JSON.stringify({ file: imageData, "Content-Disposition":attachment; filename="wgr_image.png"}) }) .then(response => { if (!response.ok) { throw new Error("Failed to upload image"); } return response.json(); })
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘uploading image to wordpress site with javascript fetch function failed’ is closed to new replies.