Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using uploadFile with Blob #44

Closed
subatomicceo opened this issue Jun 15, 2016 · 2 comments
Closed

Using uploadFile with Blob #44

subatomicceo opened this issue Jun 15, 2016 · 2 comments

Comments

@subatomicceo
Copy link

subatomicceo commented Jun 15, 2016

Ok so I'm running Meteor, with Dropzone, and uploading images to an AWS S3 bucket. After the Dropzone callback I have an image resizer which returns a Blob. I'm trying to base64 encode, and upload the Blob. Everything seems to work fine but when previewing the image it shows as a blank square? I'm assuming there is an error with my base64 encoded string? I've included some example code to help illustrate my problem. Any help would be greatly appreciated thanks!

const wordpress = require( "../" );

let client = wordpress.createClient({
    url: "my-site.com",
    username: "admin",
    password: "secret"
});

function blobToBits(blob, callback){
    let reader;
    reader = new FileReader();
    reader.onload = callback;
    reader.readAsDataURL(blob);
}

IMAGERESIZER({
    callback: function(blob){
        blobToBits(blob, function(e){
            let bits = btoa(e.target.result),
            params = { name: blob.name, type: blob.type, bits: bits}; 
            client.uploadFile(params, function( error, data ) {
                console.log( error, data );
            });
        });
    }
});
@eg-ops
Copy link

eg-ops commented Jun 16, 2016

bits should be a Uint8Array, try this

params = { name: blob.name, type: blob.type, bits: new Buffer(e.target.result)}; 

@subatomicceo
Copy link
Author

subatomicceo commented Jun 22, 2016

@g-gabber thanks so much! Just a note to anyone else attempting the same thing you have to encode the blob to base64, and then to a Unit8array.

const wordpress = require( "../" );

let client = wordpress.createClient({
    url: "my-site.com",
    username: "admin",
    password: "secret"
});

function blobToBits(blob, callback){
    let reader;
    reader = new FileReader();
    reader.onload = callback;
    reader.readAsDataURL(blob);
}

IMAGERESIZER({
    callback: function(blob){
        blobToBits(blob, function(e){
            let bits = e.target.result.replace(/^data:image\/\w+;base64,/, ''),
            params = { name: blob.name, type: blob.type, bits: ''};
           params.bits =  new Buffer(bits, 'base64');
            client.uploadFile(params, function( error, data ) {
                console.log( error, data );
            });
        });
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants