document.getElementById('fileInput').addEventListener('change', function(event) {
let previewContainer = document.getElementById("previewContainer");
previewContainer.innerHTML = ""; // Clear previous previews
let files = event.target.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
if (file.type === "application/pdf") {
let previewItem = document.createElement("div");
previewItem.classList.add("preview-item");
let iframe = document.createElement("iframe");
iframe.src = URL.createObjectURL(file);
iframe.width = "200";
iframe.height = "250";
previewItem.appendChild(iframe);
previewContainer.appendChild(previewItem);
} else {
alert("Only PDF files are allowed.");
}
}
});
function uploadFiles() {
let fileInput = document.getElementById("fileInput");
let files = fileInput.files;
let userCode = "user_" + Math.random().toString(36).substr(2, 6); // Unique ID
if (files.length === 0) {
alert("Please select files to upload.");
return;
}
let formData = new FormData();
formData.append("userCode", userCode);
formData.append("colorOption", document.getElementById("colorOption").value);
formData.append("pageSize", document.getElementById("pageSize").value);
formData.append("orientation", document.getElementById("orientation").value);
for (let i = 0; i < files.length; i++) {
let file = files[i];
let newFileName = `${file.name.split('.')[0]}_${pageSize}_${colorOption}_${orientation}.pdf`;
formData.append("files[]", file, newFileName);
}
fetch("/wp-admin/admin-ajax.php?action=upload_files", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert("Files uploaded successfully!");
} else {
alert("Upload failed.");
}
})
.catch(error => console.error("Error:", error));
}
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("uploadButton").addEventListener("click", function () {
let fileInput = document.getElementById("fileInput");
let colorOption = document.getElementById("colorOption").value;
let pageSize = document.getElementById("pageSize").value;
let orientation = document.getElementById("orientation").value;
let files = fileInput.files;
if (files.length === 0) {
alert("Please select files to upload.");
return;
}
let formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append("pdfFiles[]", files[i]);
}
formData.append("color", colorOption);
formData.append("page_size", pageSize);
formData.append("orientation", orientation);
formData.append("action", "rollinkart_upload");
fetch(ajaxurl, {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
let previewContainer = document.getElementById("previewContainer");
previewContainer.innerHTML = `Upload Successful! Your user code: ${data.user_code}
`;
data.files.forEach(file => {
let filePreview = document.createElement("div");
filePreview.innerHTML = `