Skip to content

Instantly share code, notes, and snippets.

@ntwcklng
Last active August 27, 2023 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntwcklng/f57b03c15e91c25e2cb5 to your computer and use it in GitHub Desktop.
Save ntwcklng/f57b03c15e91c25e2cb5 to your computer and use it in GitHub Desktop.
<html>
<body>
<pre id="display"></pre>
<script src="//cdn.jsdelivr.net/pouchdb/latest/pouchdb.min.js"></script>
<script src="index.js"></script>
<input type="file" id="inputFile">
<div id="img_meta_data">Please upload a File</div>
</body>
</html>
document.addEventListener('DOMContentLoaded', function () {
var inputFile = document.querySelector('#inputFile');
var imageMetaData = document.querySelector('#img_meta_data');
var getFile;
function fileUpload() {
getFile = inputFile.files[0];
// getFile is now a Blob
// Destroy the database before doing anything, because I want
// you to see the same thing if you reload.
// Ignore the man behind the curtain!
new PouchDB('sample').destroy().then(function () {
return new PouchDB('sample');
}).then(function (db) {
//
// IMPORTANT CODE STARTS HERE
//
db.put({
_id: 'image',
_attachments: {
"file": {
type: getFile.type,
data: getFile
}
}
}).then(function () {
return db.getAttachment('image', 'file');
}).then(function (blob) {
var url = URL.createObjectURL(blob);
var img = document.createElement('img');
img.src = url;
document.body.appendChild(img);
var fileSize = JSON.stringify(Math.floor(blob.size/1024));
var contentType = JSON.stringify(blob.type);
imageMetaData.innerText = 'Filesize: ' + fileSize + 'KB, Content-Type: ' + contentType;
}).catch(function (err) {
console.log(err);
});
//
// IMPORTANT CODE ENDS HERE
//
});
}
// wait for change, then call the function
inputFile.addEventListener('change', fileUpload, false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment