Skip to content

Instantly share code, notes, and snippets.

@akshay-ranganath
Created December 9, 2019 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshay-ranganath/2a195eb33890c44a7a21b3bb3d90ad46 to your computer and use it in GitHub Desktop.
Save akshay-ranganath/2a195eb33890c44a7a21b3bb3d90ad46 to your computer and use it in GitHub Desktop.
uploader.js - a helper method to support uploading the files to cloudinary
export const upload = (svgImage) => {
var url = uploadFile(svgImage)
console.log('Secure url is: ' + url)
return url.replace('.svg', '.png').replace('/upload/', '/upload/bo_3px_solid_rgb:00390b/b_white/l_cloudinary_icon,w_0.1,g_south_east,o_70/')
}
// *********** Upload file to Cloudinary ******************** //
function uploadFile (svgImage) {
var url = 'https://api.cloudinary.com/v1_1/mermaid/upload'
var xhr = new XMLHttpRequest() // eslint-disable-line
var fd = new FormData() // eslint-disable-line
xhr.open('POST', url, false)
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
// fd.append('public_id', publicId)
fd.append('upload_preset', 'mermaid')
fd.append('tags', 'Mermaid')
fd.append('file', svgImage)
xhr.send(fd)
var cldUrl = ''
if (xhr.status === 200) {
// File uploaded successfully
var response = JSON.parse(xhr.responseText)
cldUrl = response.secure_url
console.log('File uploaded: ' + cldUrl)
}
return cldUrl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment