使用UpdateHandler在Keystone.js中上载LocalFile

Uploading LocalFile in Keystone.js using UpdateHandler

本文关键字:上载 LocalFile js Keystone UpdateHandler 使用      更新时间:2023-09-26

如何做到这一点?在这里,有人建议使用UpdateHandler或profileImage._.uploadImage(file, update, callback)中列表项的下划线方法来处理文件上载,但他们使用S3 File而不是LocalFile。我首先尝试使用下划线方法,但它似乎不像上面的问题那样有效。其他网站上有很多零散的文档,但它们似乎都过时了(例如,有些人建议使用已经不存在的LocalFile方法),并且包含许多断开的链接。

查看js(中间件)代码:

view.on('post', { action: 'edit-profile' }, next => {       
    let updater = locals.user.getUpdateHandler(req, res, {
        errorMessage: 'There was an error editing your profile:'
    });
    updater.process(req.body, {
        flashErrors: true,
        logErrors: true,
        fields: 'profileImage, email, name, password'
    }, err => {
        if (err) {
            locals.validationErrors = err.errors;
            next();
        } else {
            req.flash('success', 'Profile updated');
            res.redirect('/profile');
        }
    });
});

翡翠代码:

mixin edit-profile()
form(method='post', enctype='multipart/form-data', autocomplete='off', novalidate).form-horizontal.create-form.profile-form
    input(type='hidden', name='action', value='edit-profile')
    .row: .col-sm-8.col-sm-offset-2
        h2 Edit Profile
        .form-group
            +user-picture(locals.user)
            label(for="profileImage") Change profile picture
            input(type='file', id='profileImage' name='profileImage')
        .form-group
            label(for="email") Email
            input(type='email', value=locals.user.email, size="40", id='email', name='email', placeholder='email').input.input-xl.input-faded
        .form-group
            label(for="email") Name
            input(type='text', value=locals.user.name.first, name='name.first', placeholder='First name').input.input-xl.input-faded
            input(type='text', value=locals.user.name.last, name='name.last', placeholder='Last name').input.input-xl.input-faded
        .form-group
            label(for="password") Change password
            input(type='password', id="password", name='password', placeholder='password').input.input-xl.input-faded
            input(type='password', name='password_confirm', placeholder='password').input.input-xl.input-faded
        .form-group
            button(type='submit', data-loading-text="Changing...").btn.btn-lg.btn-primary.btn-block Update profile

事实证明,在LocalFile上传时,Keystone.js对输入字段的名称非常挑剔。它在Keystone网站上的任何地方都没有文档(事实上,UpdateHandler似乎什么都没有),但您需要在名称后面加上_upload

因此,Jade代码应该是这样的(注意第一个.form-group中的差异):

mixin edit-profile()
form(method='post', enctype='multipart/form-data', autocomplete='off', novalidate).form-horizontal.create-form.profile-form
input(type='hidden', name='action', value='edit-profile')
.row: .col-sm-8.col-sm-offset-2
    h2 Edit Profile
    .form-group
        +user-picture(locals.user)
        label(for="profileImage_upload") Change profile picture
        input(type='file', id='profileImage_upload' name='profileImage_upload')
    .form-group
        label(for="email") Email
        input(type='email', value=locals.user.email, size="40", id='email', name='email', placeholder='email').input.input-xl.input-faded
    .form-group
        label(for="email") Name
        input(type='text', value=locals.user.name.first, name='name.first', placeholder='First name').input.input-xl.input-faded
        input(type='text', value=locals.user.name.last, name='name.last', placeholder='Last name').input.input-xl.input-faded
    .form-group
        label(for="password") Change password
        input(type='password', id="password", name='password', placeholder='password').input.input-xl.input-faded
        input(type='password', name='password_confirm', placeholder='password').input.input-xl.input-faded
    .form-group
        button(type='submit', data-loading-text="Changing...").btn.btn-lg.btn-primary.btn-block Update profile

如果您使用的是标准HTML,那也没什么不同。只需将输入的id和名称设置为fieldnamehere_upload(在我的情况下,LocalFile的字段名称为profileImage)。更新程序代码是准确的(请注意,updater.process参数中fields的顺序应该与表单中输入字段的顺序相同)。

我不建议使用列表中的下划线方法。这似乎比使用这里显示的UpdateHandler更难。