shrmaprem0202 wrote: ↑Thu Jan 11, 2024 8:01 am
I want to redirect user to index page after upload a file how can can you give me a source code of that
Hello! I wasn't able to get the JS Wrapper library for the Wapka API to work, but anyway, here's a javascript code to allow you to upload the file and, if successful, redirect to the home page.
Code: Select all
<form action="https://api.wapka.org/FileCreator" method="post" enctype="multipart/form-data" onsubmit="return uploadFile()">
<input type="hidden" name="folderid" id="folderInput" value="69529">
<input type="hidden" name="apikey" id="apiKey" value="{{VAR(APIKEY)}}">
<input name="upload" type="file" id="fileInput">
<input type="submit" value="Upload">
</form>
<script type="text/javascript">
function uploadFile() {
let folderId = document.getElementById('folderInput').value;
let apiKey = document.getElementById('apiKey').value;
let formData = new FormData();
formData.append('folderid', folderId);
formData.append('apikey', apiKey);
formData.append('upload', document.getElementById('fileInput').files[0]);
let xhr = new XMLHttpRequest();
xhr.open('POST', 'https://api.wapka.org/FileCreator', true);
xhr.setRequestHeader('Accept', 'application/json');
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
let responseData = JSON.parse(xhr.responseText);
console.log(responseData);
if (responseData.ok) {
window.location.href = '/';
} else {
console.error('Upload error:', responseData.error_type, responseData.description);
alert('Error uploading file. Please try again. Details: ' + responseData.description);
}
} else {
console.error('Request failed with status:', xhr.status);
alert('Error during file upload. Please try again.');
}
};
xhr.onerror = function () {
console.error('Network error during the request.');
alert('Error during file upload. Please try again.');
};
xhr.send(formData);
return false;
}
</script>
The relevant part is this, you can change '/' to any page you want to redirect, for example '/somepage', or even redirect to another website, for example 'https://google.com'.
Code: Select all
if (responseData.ok) {
window.location.href = '/';
}