node.js - mongodb gridfs encoding picture base64 -


i try readout image, saved in mongodb, via gridfs (without temporary file) should directly sent ajax, injects html

when use actual functions large bit string formed , sent client (is saved in ajax response var)

but reaches client, bits arent correct anymore

so way encode picture before sent (into base64) (or there other way?)

serverside - javascript, gridfs

exports.readfilefromdb = function(req, res, profile, filename, callback){     console.log('find data profile ' + json.stringify(profile));      var gridreader = new gridstore(db, filename,"r");      gridreader.open(function(err, gs) {          var streamfile = gs.stream(true);          streamfile.on("end", function(){          });          // pipe out data         streamfile.pipe(res);     gridreader.close(function(err, result) {      }); 

clientside - javascript ajax call:

function imgupload(){      var thumb = $("#previewpic");      $('#uploadform').ajaxsubmit({          beforesend:function(){             //launchpreloader();         },          error: function(xhr) {             //status('error: ' + xhr.status);         },          success: function(response) {             console.log(response);             var imagedata = $.base64encode(response);             console.log(imagedata);             thumb.attr("src","data:image/png;base64"+imagedata);             $("#spanfilename").html("file uploaded")         }     }); } 

i'm doing similar current project, when upload complete, return json object containing url uploaded image:

{ success : true, url : '/uploads/gridfsid/filename.ext' } 

i have route in express handles /uploads route retrieve file gridfs , stream client, can use above url in img src. appears in dom:

<img src="/uploads/gridfsid/filename.ext"> 

the route handler looks (it uses node-mime , gridfs-stream):

app.get(/^\/uploads\/([a-f0-9]+)\/(.*)$/, function(req, res) {   var id        = req.params[0];   var filename  = req.params[1];    // set correct content type.   res.set('content-type', mime.lookup(filename));    // find gridfs file id , pipe response stream.   gridfs     .createreadstream({ _id : id })     .on('error', function(err) {       res.send(404); // or 500     })     .pipe(res); }); 

it depends on exact setup if solution works you.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -