Help Center/
Object Storage Service/
SDK Reference/
BrowserJS/
FAQs/
How Do I Upload a Base64-Encoded Image?
Updated on 2025-04-03 GMT+08:00
How Do I Upload a Base64-Encoded Image?
You need to convert a Base64-encoded image into a specified format, and then call the OBS upload API to upload the image.
const base64ImgtoFile = function base64ImgtoFile(base64Content, filename) {
const arr = base64Content.split(',');
const mime = arr[0].match(/:(.*?);/)[1];
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
// If the environment supports file formats, you can also use return new File([u8arr], filename, { type: mime }).
return new Blob([u8arr], { type: mime });
};
// obsClient indicates an instance of the OBS client.
const uploadBase64Img = function uploadBase64Img(obsClient) {
// Specify the content in Base64 format.
const base64Content = "data:image:xxxxxxxxxxxxx";
const filename = 'img.png';
const imgfile = base64ImgtoFile(base64Content, filename);
obsClient.putObject({
Bucket: 'bucketname',
Key: filename,
SourceFile: imgfile
}, function (err, result) {
if (err) {
console.error('Error-->' + err);
} else {
console.log('Status-->' + result.CommonMsg.Status);
}
});
};
Parent topic: FAQs
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.
The system is busy. Please try again later.