首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何显示存储在Types.File中的KeystoneJS缩略图/预览图像?和模式url与publicPath

如何显示存储在Types.File中的KeystoneJS缩略图/预览图像?和模式url与publicPath
EN

Stack Overflow用户
提问于 2017-02-18 04:03:54
回答 1查看 893关注 0票数 0

我正在使用Types.File存储本地图像,我有两个问题,如果有人能帮我的话!

1)我在'fs‘中定义了我的文件名,并将模式url设置为true,我原以为url会返回包含publicPath的内容,但结果却是/public/“publicPath”,我是不是定义错了publicPath?或者它是用来做别的的?

2)我尝试使用Types.File和Types.Url的format属性将图片标签作为缩略图/预览返回,但这两个都没有成功,我注意到format被移除并将在Types.File中重新实现它还没有吗??还是我在这里也做错了什么?

代码语言:javascript
运行
复制
 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(); 
 });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-26 08:59:18

对于你的第一期,我有同样的事情。原来FS适配器使用https://nodejs.org/api/url.html#url_url_resolve_from_to来创建url字符串(下面的代码取自keystone的FS适配器):

代码语言:javascript
运行
复制
/**
    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文档页面上的示例中所看到的:

代码语言:javascript
运行
复制
url.resolve('/one/two/three', 'four')         // '/one/two/four'

所以只需在你的/postimages后面加上一个"/“,它就应该可以工作了。

代码语言:javascript
运行
复制
publicPath: '/postimages/'

在此之后,事情应该会正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42306454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档