After attempting various solutions without success, I finally found a method that worked for me in uploading files.
The setup involved having a file selection input along with a submit button:
<input type="file" name="file" id="file">
<button onclick="doupload()" name="submit">Upload File</button>
Subsequently, I added this JavaScript function to handle the upload process:
function doupload() {
let data = document.getElementById("file").files[0];
let entry = document.getElementById("file").files[0];
console.log('doupload',entry,data)
fetch('uploads/' + encodeURIComponent(entry.name), {method:'PUT',body:data});
alert('Your file has been successfully uploaded');
location.reload();
};
If you're a fan of Stack Snippets...
The PUT
method differs slightly from the POST
method. For instance, the POST
method might not be supported on certain web servers such as in Chrome.
I tested this functionality using the Web Server for Chrome extension -
Link Here
Remember - With the Web Server for Chrome, ensure you enable 'file upload' in the advanced settings to prevent any errors related to permissions.