<!DOCTYPE html>
<html lang="it">
<head>
<title>Upload with PUT</title>
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
<label for="customFile"
x-data="{
file: null,
myfile: '',
message: '',
show_select: true,
show_send: false,
async sendFile() {
file = this.file;
this.file = null;
this.show_select = false;
this.show_send = false;
this.message = 'Carico il file...';
response = await (await fetch('http://api.example.com/something', {
method: 'PUT',
body: file,
headers: {'Content-Type': 'application/octet-string'},
}));
if (response.ok) {
this.message = 'Upload completed';
}
else {
this.message = 'Upload failed!!!';
}
outerthis = this;
setTimeout(
function(){
outerthis.message = '';
outerthis.show_select = true;
}, 3000);
},
}">
<input type="file" style="display: none" id="customFile"
x-on:change="file = Object.values($event.target.files)[0]; show_send = true">
<span x-show="show_select" x-transition x-text="file ? file.name : 'Click to select a file...'"></span>
<button x-show="show_send" x-transition x-on:click="sendFile()">Send</button>
<span x-text="message"></span>
</label>
</body>
</html>