The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
I am trying to commit a png file to a bitbucket cloud repo via Bitbucket API using javascript, not using any js dependency as I am not running it as server but just on browser.
Commit image:
Also the same with getting the image:
Welcome to the community.
Based on my understanding, you wanted to upload a PNG file from a browser to your Bitbucket Cloud repository.
If that's correct, according to our API documentation here you'll have to use the multipart/form-data as your MIME type.
Also, you will also need to obtain the image source before you can upload it (e.g. Upload input tag data).
As an example, below I used HTML and Jquery to upload a file to my repository:
HTML:
<input type="file" id="filetag"> <img src="" id="preview">
JQuery:
var fileTag = document.getElementById("filetag"), preview = document.getElementById("preview"); fileTag.addEventListener("change", function() { changeImage(this); }); function changeImage(input) { var reader; if (input.files && input.files[0]) { reader = new FileReader(); reader.onload = function(e) { preview.setAttribute('src', e.target.result); } reader.readAsDataURL(input.files[0]); const form = new FormData(); form.append("some_image.png", input.files[0]); form.append("branch", "master"); form.append("message", "Test upload"); const settings = { "async": true, "crossDomain": true, "url": "https://api.bitbucket.org/2.0/repositories/workspace_id/repo_name/src", "method": "POST", "headers": { "Authorization": "Basic <SOME_CREDENTIALS_HERE>" }, "processData": false, "contentType": false, "mimeType": "multipart/form-data", "data": form }; $.ajax(settings).done(function (response) { console.log(response); }); } }
To get the PNG file from your repository via API, the response would be in binary.
Hence, you will need to convert that binary response first before you can use it in your image src attribute.
Hope it helps and let me know how it goes.
Regards,
Mark C
Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.