简体   繁体   中英

Store an uploaded image to Github using NodeJS and Github API

Been Struggling to try to push an Image to my repo on Github using NodeJS and Github API, I've covered the part of creating the SHA,Commit Tree and everything, the only thing left is to pass the image to the API and saving it as an actual Image (eg test.png)

What I did so far is retrieving the image like below:

reader.onload = (e) => { image = reader.result.split(',')[1] }

Then sending it to the NodeJS server to get Sent to Github, but it's only stored as test.jpg but the content is not an actual Image but just :

/9j/4AAQSkZJRgABAQAASABIAAD/4QBARXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqA.....

Any help?

Based on this blog post , you might have to add ?raw=true to the end of the src url. Alternately, I found a hack on a previous SO answer .

Okay solved it! the only thing that I had to add is to actually convert the base64 content to Binary using this:

var data = Buffer.from(b64string, 'base64');

And then sending this data to a commit which worked perfectly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM