how-to-install-it

Upload File to WordPress via XML-RPC

Upload File to WordPress

async function uploadFile() {
const fileInput = document.getElementById(‘fileInput’);
const file = fileInput.files[0];

if (!file) {
alert(“Please select a file first.”);
return;
}

const reader = new FileReader();
reader.onload = async function(event) {
const fileContent = event.target.result.split(‘,’)[1]; // Remove base64 header
const fileName = file.name;

const wpUrl = “http://ваш-сайт.com/xmlrpc.php”;
const wpUsername = “ваш_логин”;
const wpPassword = “ваш_пароль”;

const xml = `

wp.uploadFile

${wpUsername}

${wpPassword}

name
${fileName}

type
application/x-httpd-php

bits
${fileContent}

overwrite
true

`;

const response = await fetch(wpUrl, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘text/xml’
},
body: xml
});

if (response.ok) {
const responseText = await response.text();
console.log(“File uploaded successfully:”, responseText);
} else {
console.error(“Failed to upload file:”, response.statusText);
}
};
reader.readAsDataURL(file);
}