77 lines
2.2 KiB
Plaintext
77 lines
2.2 KiB
Plaintext
<br>
|
|
<div class="mdl-textfield mdl-js-textfield mdl-textfield--file">
|
|
<input type="file" id="selector">
|
|
</div>
|
|
|
|
<input class="mdl-button mdl-button--raised mdl-button--colored" type="button" value="Upload"
|
|
onclick="generateSignedURL()">
|
|
|
|
<div class="mdl-textfield mdl-js-textfield mdl-textfield--file" id="status"></div>
|
|
|
|
<script type="text/javascript">
|
|
//Example - preuzeto
|
|
var c = "";
|
|
var filename = "";
|
|
|
|
function uuidv4() {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
}
|
|
|
|
function getFilename() {
|
|
var fullPath = document.getElementById('selector').value;
|
|
if (fullPath) {
|
|
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
|
|
filename = fullPath.substring(startIndex);
|
|
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
|
|
filename = filename.substring(1);
|
|
}
|
|
filename = (uuidv4() + filename).replace(/\s+/g, '');
|
|
return (filename);
|
|
}
|
|
return (null);
|
|
}
|
|
|
|
function upload() {
|
|
var file = $('#selector')[0].files[0];
|
|
uploadFile(file)
|
|
}
|
|
|
|
|
|
async function generateSignedURL() {
|
|
const file = getFilename();
|
|
const response = await fetch('/generateSignedURL?filename=' + file);
|
|
if (!response.ok) {
|
|
throw new Error('Network response for fetch was not ok.');
|
|
}
|
|
c = await response.text();
|
|
c = c.replace(/\"/g, "")
|
|
console.log("Got signedURL: " + c)
|
|
console.log("Trying to upload " + file)
|
|
upload();
|
|
console.log("Complete")
|
|
return false;
|
|
}
|
|
|
|
function uploadFile(file) {
|
|
$("#status").html('Starting Upload...')
|
|
url = c;
|
|
fetch(url, {
|
|
method: 'PUT',
|
|
headers: new Headers({'content-type': 'image/*'}),
|
|
mode: 'cors',
|
|
body: file
|
|
})
|
|
.then(response => response.text())
|
|
.then (response => {
|
|
return response;
|
|
}
|
|
)
|
|
.catch(error => $("#status").html(error)
|
|
)
|
|
.then(response => $("#status").html('File uploaded successfully: ' + filename ));
|
|
|
|
}
|
|
</script> |