Bitbucket API commit image and base64 encoding Edited

I'm using nodejs to push up an image to a repo from the src endpoint using multipart/form post and no matter how I send it, binary, text, raw it's always unusable after upload.  What is the proper way to upload an image from node?  I have successfully pushed plain text files but not images/binary data.  I am using the npm FileReader package to read the file contents and I've tried

readAsArrayBuffer
readAsBinaryString
readAsText

 but nothing seems to work.  I'm using the npm package "form-data" to do the form post which works fine for regular text files.

Also is base64 encoding supported?  It doesn't appear to be supported because the api doesn't decode the file after upload and I can't send the "Content-Transfer-Encoding": "base64" header because it's not allowed from the CORS pre-flight request.  Seems like base64 encoding is important for multipart form posts of binary data.

Any help appreciated!

2 answers

1 accepted

In case anybody is looking for this here is the function we needed to make our uploaded binary files work, thanks to @taussoe for the snippet!

Check out netlify cms for the implementation soon.

dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);

// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);

// create a view into the buffer
var ia = new Uint8Array(ab);

// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}

// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: mimeString});
return blob;

}

 

 

 

Crickets :)  Anybody have any ideas here?  Anybody uploading binary data using node/javascript to the bitbucket api?

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

643 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot