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
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.