【问题标题】:KeystoneJS Cloudinary image uploadKeystoneJS Cloudinary 图片上传
【发布时间】:2016-09-20 02:48:04
【问题描述】:

我正在使用最新版本的KeystoneJS 并且有一个表单可以向数据库中添加记录。 我无法正常上传图片。

我的模型包含:

heroImage: { type: Types.CloudinaryImage, autoCleanup : true },

我的表格包括:

<input type="file" accept="image/*" id="heroImage" name="heroImage_upload" className='field-upload'>

我保存表单的中间件只包括:

view.on('post', {action: 'save'}, function(next) 
{
    var newProperty = new Property.model(req.body);

    console.log(newProperty);
    newProperty.save(function(err, body) 
    {});
});

它适用于除文件上传之外的所有领域。

我已经尝试添加:

    newProperty.heroImage = req.files['heroImage'];

heroImage 保留为空。

我也尝试创建cloudinaryImage,但这会导致错误:

var img = new CloudinaryImage(req.files['heroImage']);

当我使用KeystoneJS 管理仪表板上传图像时,一切正常。有人可以解释一下我应该如何在自己的表单中使用 cloudinaryImage 字段类型吗?

谢谢

【问题讨论】:

    标签: express mongoose cloudinary keystonejs


    【解决方案1】:

    不确定这是否会有所帮助,但文档底部有一条注释: http://keystonejs.com/docs/database/#fieldtypes-cloudinaryimage

    “请记住,如果您使用 HTML 表单将图像上传到 CloudinaryImage 字段,则需要在 form 标记中指定 enctype="multipart/form-data"。”

    您是否将其包含在您的表单中?

    更新:

    因此,假设您的模型名为 MyModel,并且您的表单数据使用与您的模型相同的对象键,则以下内容应该有效。 (即您的 heroImage 字段的图像应在 POST 数据中以 heroImage 的形式提供)。

    var MyModel = keystone.list('MyModel');
    
    view.on('post', {action: 'save'}, function(next) 
    {
       var item = new MyModel.model();
       data = req.body;
       item.getUpdateHandler(req).process(data, function(err) {
          // Handle error
       }
    }
    

    然后,Keystone 应在内部处理所有特定的云计算内容。

    【讨论】:

    • 是的,我有一个多部分表单。事实上,如果我在属性对象上使用getUpdateHandler(req).process(req.body, function),它可以正常工作,但这会导致其他验证问题。我想使用save()方法,但认为将上传的文件转换为CloudinaryImage对象以将其上传到Cloudinary需要更多
    • 嗯,恐怕我不确定。您可以在 view.on('post' 函数中尝试类似于 var cloudinary = require('cloudinary'); cloudinary.uploader.upload(req.files['heroImage'], function(result) { ...}); 的内容。您可能还必须将您的 cloudinary 配置选项传递给它才能使其工作。
    • 如果可行,您还需要告诉您 keystone 模型图像在 cloudinary 上的位置。类似于将模型上的path 设置为来自cloudinary 的result。不过,这应该正是更新处理程序为您所做的。您遇到了什么样的验证错误?
    • 查看getUpdateHandler 似乎它调用了CloudinaryImagegetRequestHandler(item, req, paths, callback),但没有文档说明itempaths 应该是什么,或者如何调用它。
    • property.heroImage.getRequestHandler( 给出错误property.heroImage.getRequestHandler is not a function
    【解决方案2】:

    这是一种使用cloudinary.v2的方法

    未经测试的基石

    cloudinary.v2.uploader.upload(base64_file_data, (err, result) => {
    
      var newMyModel = new MyModel.model(model_data);
    
      newMyModel.image = result;
    
      let updater = newMyModel.getUpdateHandler(req);
    
      updater.process(newMyModel, {
        fields: image
      }, err => {...})   
    })
    

    经过测试的猫鼬

    cloudinary.v2.uploader.upload(base64_file_data, (err, result) => {
    
      var newMyModel = new MyModel.model(model_data);
    
      newMyModel.image = result;
    
      newMyModel.save(err => {...})   
    })
    

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 2015-12-19
      • 2021-04-22
      • 2022-01-12
      • 2017-02-23
      • 2019-03-17
      • 2021-09-29
      • 2019-07-13
      • 2013-10-07
      相关资源
      最近更新 更多