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

Example for uploading an image from a canvas #17

Closed
mkpazon opened this issue Apr 26, 2016 · 3 comments
Closed

Example for uploading an image from a canvas #17

mkpazon opened this issue Apr 26, 2016 · 3 comments

Comments

@mkpazon
Copy link

mkpazon commented Apr 26, 2016

I need help uploading an image from a canvas. Right now this is what I have.


   var imageDataURL = canvas.toDataURL();


    var b2 = new B2({
      accountId: B2_ACCOUNT_ID,
      applicationKey: B2_APPKEY
    });

    b2.authorize().then(function() {
      console.log(".authorize-DONE");
      return b2.getUploadUrl(B2_BUCKET_ID);
    }).then(function(response) {
      console.log(".uploadUrl:" + JSON.stringify(response));
      return b2.uploadFile({
        uploadUrl: response.uploadUrl,
        uploadAuthToken: response.authorizationToken,
        mime: 'image/png',
        filename: 'sample.png',
        data: imageDataURL    // You mentioned to use a Buffer but I am clueless how to do it. Example please.
      });
    }).then(function(response) {
      console.log(JSON.stringify(response));
    }).catch(function(error) {
      console.log("A problem has occurred:" + error.stack);
    });

@mkpazon
Copy link
Author

mkpazon commented Apr 26, 2016

Answering my own question:

    var imageDataURL = canvas.toDataURL();
    var byteString = imageDataURL.split(',')[1];
    var buf = new Buffer(byteString, 'base64');

    var b2 = new B2({
      accountId: B2_ACCOUNT_ID,
      applicationKey: B2_APPKEY
    });

    b2.authorize().then(function() {
      console.log(".authorize-DONE");
      return b2.getUploadUrl(B2_BUCKET_ID);
    }).then(function(response) {
      console.log(".uploadUrl:" + JSON.stringify(response));
      return b2.uploadFile({
        uploadUrl: response.uploadUrl,
        uploadAuthToken: response.authorizationToken,
        filename: 'sample2.png',
        data: buf
      });
    }).then(function(response) {
      console.log(JSON.stringify(response));
    }).catch(function(error) {
      console.log("A problem has occurred:" + error.stack);
    });

@ablankenship10
Copy link

Looks like this can be closed as its not an issue with the package and OP has provided a nice example of the process.

@yakovkhalinsky
Copy link
Owner

Yep thought I'd leave it open for a response 👍

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

3 participants