Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Bitbucket API commit image and base64 encoding

zanedev August 1, 2017

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!

3 answers

1 accepted

0 votes
Answer accepted
zanedev August 11, 2017

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;

}

 

 

 

0 votes
guruprasad mahale October 6, 2021

Hi @zanedev  any luck yet with commiting image to Bitbucket via api?

0 votes
zanedev August 6, 2017

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

NerdyDeeds May 26, 2019

Aaaaaand a one. I am! At least, now I found how to convert the damn images to blobs! Thank you Zane!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events