I'm using Node.js and Express. I just implement Cloudinary to upload image from user, and store the image id to my MySQL database.
I tried this tutorial from Cloudinary's documentation: http://cloudinary.com/documentation/node_image_upload. Everything works well, except at the final step, I need to get the image_id
.
var url = require('url')
var url_parts = url.parse(req.body.coverurl, true)
console.log(url_parts) //DEBUG
var query = url_parts.query
console.log(query) //DEBUG
var preloaded_file = new cloudinary.PreloadedFile(query.image_id)
if (preloaded_file.is_valid()) {
var image_id = preloaded_file.identifier()
console.log("Img ID:" + image_id)
} else {
console.log("Invalid upload signature")
}
The req.body.coverurl
is something like: image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862
The url_parts
is:
{ protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: '#f63f1fe77c7f536447e079f3dd10829b5a15d862',
search: '',
query: {},
pathname: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png',
path: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png',
href: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862' }
And..., variable query
is empty. query.image_id
is invalid so can't get the image id, also can't verify the signature.
I don't know why Cloudinary give this invalid callback string. Or how can I solve this problem.
Thank for your help. And forgive me for my bad English :)
Update
my submit_idea.hbs
<form action="/submit/idea" method="POST">
{{{imgtag}}} <!-- this will render from: cloudinary.uploader.image_upload_tag('coverurl') -->
<button type="submit">Save</button>
</form>
in my routes/submit.js
router.get('/submit/idea', function(req, res, next){
// just render the submit_idea.hbs with some Cloudinary variable
})
router.post('/submit/idea', function(req, res, next){
var url = require('url')
var url_parts = url.parse(req.body.coverurl, true)
var query = url_parts.query
var preloaded_file = new cloudinary.PreloadedFile(query.image_id)
if (preloaded_file.is_valid()) {
var image_id = preloaded_file.identifier()
console.log("Img ID:" + image_id)
} else {
console.log("Invalid upload signature")
}
// add image_id to mysql
})