Preview of image database field works just with .png

107 Views Asked by At

I can upload images to the server with this code and with the krajee Bootstrap FileInput plugin, but when i try to visualize it, only .png shows an image.

-------------------------Code-------------------------

    var currentFolder = ds.getDataFolder().path+"tmp."+extension; 
    var buffer2 = new Buffer(base64Texto,'base64');
    var vBlob2 = buffer2.toBlob();
    debugger;
    vBlob2.copyTo(currentFolder,"OverWrite")
    var imagen = loadImage(currentFolder);
    archivo.Previsualizacion = imagen;
    archivo.save();

EDIT:

The problem is that when i store the image(.jpg or .pdf) in a image field in the database, preview is blank, but the file from the Blob is well created. With .png it works perfectly.

     var bufferFrom64 = new Buffer(base64Texto,'base64');
     var vBlob2 = bufferFrom64.toBlob(mimeType);
     vBlob2.copyTo(currentFolder,"OverWrite")
     var imagen = loadImage(currentFolder);
     archivo.Previsualizacion = imagen;
     archivo.Documento = vBlob2;
     archivo.save(); 
1

There are 1 best solutions below

0
Xiang Liu On

In short, Wakanda buffer.toBlob() does not play nice with image type especially .jpg or .pdf. .png images have different implementation and will work with toBlob().

This is the code I use to decode images encoded in base64:

   var currentFolder = ds.getDataFolder().path+"tmp."+extension; 
   var bufferFrom64 = new Buffer(base64Texto, "base64");
   var outputFile   = File(currentFolder);
   if(!outputFile.exists)outputFile.create();
   var writestream = BinaryStream(outputFile, "Write");
   writestream.putBuffer(bufferFrom64);
   writestream.flush();
   writestream.close();

Hope it helps.