首页 文章

如何在node.js上实现jQuery直接上传到Cloudinary

提问于
浏览
8

我正在尝试通过他们的jQuery插件直接上传到Cloudinary到Node.js应用程序,我想知道是否有人可以帮我填补他们的例子中的一些空白 .

这是博客文章,解释了如何做到这一点:

http://cloudinary.com/blog/direct_image_uploads_from_the_browser_to_the_cloud_with_jquery

在“其他开发框架和高级用法”部分中,我坚持的部分是:

  • "Set data-form-data to be JSON representation of the upload API params. The mandatory fields are api_key, timestamp, signature and callback.",

更具体地说:

  • "The signature needs to be generated on the server-side for correct authentication."

似乎没有任何确切如何实现这一目标的例子 .

例子是:

<input name="file" type="file" 
       class="cloudinary-fileupload" data-cloudinary-field="image_upload" 
       data-form-data=" ... html-escaped JSON data ... " ></input>

并且数据表单数据的未转义JSON内容是:

{ "timestamp":  1345719094, 
  "callback": "https://www.example.com/cloudinary_cors.html",
  "signature": "7ac8c757e940d95f95495aa0f1cba89ef1a8aa7a", 
  "api_key": "1234567890" }

你如何产生签名?我知道我需要在node.js中执行此操作,看起来它需要在生成表单时发生,尽管我可以告诉签名需要包含时间戳 - 这肯定会在用户发布时过时填写了表格?

请求身份验证的文档位于:http://cloudinary.com/documentation/upload_images#request_authentication

在我正在使用的cloudinary_npm模块中, uploader.coffee 中有一个名为 direct_upload 的方法,它似乎是帮助实现这一点的方法,但我不清楚如何将它们实际绑定在一起 .

这两个框架在博客文章Rails和Django中有真实的例子,通过他们自己的帮助者,例如,在Django中,您将 {{ form.image }} 添加到表单中,该表单从 image = cloudinary.forms.CloudinaryJsFileField() 输出结果 - 遗憾的是,如何在任何其他服务器端环境中复制此内容都不包括在内 .

如果有人能够对此有所了解,或者分享关于如何使其端到端工作的要点或示例,我将非常感激 .

2 回答

  • 2

    请查看https://github.com/cloudinary/cloudinary_npm/blob/67b7096c7fac2c2bed06603912966495d59dfa34/lib/uploader.coffee#L220处的uploader.image_upload_tag . 它返回输入标记的html,可以与jquery.cloudinary.js一起使用,直接将图像上传到cloudinary . 它将成为下一个npm版本的一部分(预计下周某个时候) . 至于时间戳 - 签名有效期为1小时,因此用户应该有足够的时间上传图像 .

  • 0

    如果你使用nodejs,这可能会有所帮助

    var querystring = require('querystring');
    
    var toHash = querystring.stringify(req.query) + secret;
    
    var signature = sha1(toHash);
    
    function sha1(data) {
    var generator = crypto.createHash('sha1');
    generator.update(data)
    return generator.digest('hex')
    }
    

相关问题