我正在使用Types.File存储本地图像,我有两个问题,如果有人能帮我的话!
1)我在'fs‘中定义了我的文件名,并将模式url设置为true,我原以为url会返回包含publicPath的内容,但结果却是/public/“publicPath”,我是不是定义错了publicPath?或者它是用来做别的的?
2)我尝试使用Types.File和Types.Url的format属性将图片标签作为缩略图/预览返回,但这两个都没有成功,我注意到format被移除并将在Types.File中重新实现它还没有吗??还是我在这里也做错了什么?
 var myStorage = new keystone.Storage({
     adapter: keystone.Storage.Adapters.FS,
     fs: {
         path: keystone.expandPath('./public/postimages'), // required; path where the files should be stored
         publicPath: '/postimages', // path where files will be served
     },
     schema: {
         url: true
     }
 });
 Post.add({
      *...*
      image: {
        type: Types.File,
         storage: myStorage,
         /* format: function(item, file) {
             return '<img src="/files/' + file.filename + '" style="max-width: 300px">';
         } */
       },
     image_url: {
         type: String,
        noedit: true,
     }
      *...*
 });  
 Post.schema.pre('save', function(next) {
     this.image_url = this.image ? this.image.url : '';
     next(); 
 });发布于 2017-02-26 08:59:18
对于你的第一期,我有同样的事情。原来FS适配器使用https://nodejs.org/api/url.html#url_url_resolve_from_to来创建url字符串(下面的代码取自keystone的FS适配器):
/**
    Gets the public path of a stored file by combining the publicPath option
    with the filename in the field value
*/
FSAdapter.prototype.getFileURL = function (file) {
    var publicPath = this.options.publicPath;
    if (!publicPath) return null; // No URL.
    return url.resolve(publicPath, file.filename);
};正如您从nodejs文档页面上的示例中所看到的:
url.resolve('/one/two/three', 'four')         // '/one/two/four'所以只需在你的/postimages后面加上一个"/“,它就应该可以工作了。
publicPath: '/postimages/'在此之后,事情应该会正常工作。
https://stackoverflow.com/questions/42306454
复制相似问题