javascript - How do you store an image with other data from a form? -
i'm trying use firebase store list of events. each event has typical info name, date, location, , image. tried using file api store dataurl rest of data in reference, can't seem image under same key other data. how add upload image via form other data under name id in firebase.
this handler change event on file.
function onfilechanged(theevt) { var thefile = theevt.target.files[0]; // check see if text if (!thefile.type.match("image.*")) { return; } var reader = new filereader(); reader.onload = function (evt) { var resultdata = evt.target.result; var file = thefile.name.replace(/\.[^\.]+$/, ''); nameref = new firebase('https://firebaseio.com/events/' + file); var f = nameref.child('image'); f.set(resultdata); } reader.readasdataurl(thefile); }
that snippet should work, long file represented data url (base64 encoded). here's example of how did in 1 of our sample apps: https://github.com/firebase/firepano/blob/gh-pages/firepano.js - code same except snippet generates random id file instead of extracting filename.
function handlefileselect(evt) { var f = evt.target.files[0]; var reader = new filereader(); reader.onload = (function(thefile) { return function(e) { var filepayload = e.target.result; var hash = cryptojs.sha256(math.random() + cryptojs.sha256(filepayload)); var f = new firebase(firebaseref + 'pano/' + hash + '/filepayload'); spinner.spin(document.getelementbyid('spin')); f.set(filepayload, function() { spinner.stop(); document.getelementbyid("pano").src = e.target.result; $('#file-upload').hide(); window.location.hash = hash; }); }; })(f); reader.readasdataurl(f); }
Comments
Post a Comment