使用UpdateHandler在Keystone.js中上传LocalFile

怎么会这样做呢? 在这里 ,有人build议在profileImage._.uploadImage(file, update, callback)使用UpdateHandler或列表项的下划线方法来处理file upload,但它们使用S3 File而不是LocalFile 。 首先尝试使用下划线方法,但似乎不像上述问题那样工作。 在其他网站上有很多分散的文档,但都显得过时(有些人build议使用不再存在的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 

事实certificate,问题是Keystone.js对LocalFile上传时input字段的命名非常挑剔。 它没有logging在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,那真是一回事。 只需将input的id和名称设置为fieldnamehere_upload (在我的情况下,LocalFile的字段名称是profileImage )。 更新器代码是正确的(请注意, updater.process参数中的fields顺序应与表单中input字段的顺序相同)。

我不会推荐使用列表中的下划线方法。 看起来比使用UpdateHandler更难,如下所示。