登录  /  注册
首页 > web前端 > js教程 > 正文
keystone.js后台编辑器中上传图片的实现方法
不言
发布: 2018-09-11 15:35:35
原创
1452人浏览过

本篇文章给大家带来的内容是关于keystone.js后台编辑器中上传图片的实现方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

使用keystone时遇到一个问题:keystone后台使用tinymce做富文本编辑器,但其只提供了插入网络图片的功能,而不能上传和管理本地图片,好在keystone提供了选项来为tinymce添加插件

一、 在编辑器中添加插件

1. 上传图片插件

下载插件并放到静态目录下

2. 配置

keystone.init()中增加如下配置项:

{
  'wysiwyg additional plugins': 'uploadimage',
  'wysiwyg additional buttons': 'uploadimage',
  'wysiwyg additional options': {
    'uploadimage_form_url': '/api/admin/upload-image', //上传图片的API
    'relative_urls': false,
    'external_plugins': { 'uploadimage': '/js/uploadimage/plugin.min.js' }, // 上传图片插件
  }
}
登录后复制

二、后台API

1. 监听路由

在路由文件中增加如下代码:

var router = express.Router();
var keystone = require('keystone');
var importRoutes = keystone.importer(__dirname);
var routes = {
  api: importRoutes('./api'),
};

router.post('/api/admin/upload-image', keystone.middleware.api, routes.api.upload_image);

module.exports = router;
登录后复制

我们将API放到api/upload_image.js中,注意新增的API需要添加keystone.middleware.api中间件

2. 建立新域来管理图片

models/FileUpload.js

var keystone = require('keystone');
var Types = keystone.Field.Types;

/**
 * File Upload Model
 * ===========
 * A database model for uploading images to the local file system
 */

var FileUpload = new keystone.List('FileUpload');

var myStorage = new keystone.Storage({
  adapter: keystone.Storage.Adapters.FS,
  fs: {
    path: keystone.expandPath('public/uploads'), // required; path where the files should be stored
    publicPath: 'uploads', // path where files will be served
  }
});

FileUpload.add({
  name: { type: Types.Key, index: true},
  file: {
    type: Types.File,
    storage: myStorage
  },
  createdTimeStamp: { type: String },
  alt1: { type: String },
  attributes1: { type: String },
  category: { type: String },      //Used to categorize widgets.
  priorityId: { type: String },    //Used to prioritize display order.
  parent: { type: String },
  children: { type: String },
  url: {type: String},
  fileType: {type: String}

});


FileUpload.defaultColumns = 'name';
FileUpload.register();
登录后复制

3. API细节

api/upload_image.js实现细节:

var
  keystone = require('keystone'),
  fs = require('fs'),
  path = require('path');

var FileData = keystone.list('FileUpload'); 

module.exports = function (req, res) {
  var
    item = new FileData.model(),
    data = (req.method == 'POST') ? req.body : req.query;
  
  // keystone采用的老版multer来解析文件,根据req.files.file.path将文件从缓冲区复制出来
  fs.copyFile(req.files.file.path, path.join(__dirname, '../../public/uploads', req.files.file.name), function (err) {
    var sendResult = function () {
      if (err) {
        res.send({ error: { message: err.message } });
      } else {
        // 按插件要求的返回格式返回URL
        res.send({ image: {
          url: "\/uploads\/" + req.files.file.name
        } });
      }
    };

    // TinyMCE upload plugin uses the iframe transport technique
    // so the response type must be text/html
    res.format({
      html: sendResult,
      json: sendResult,
    });
  })
}
登录后复制

相关推荐:

请教编辑器ckeditor开启图片上传后 upload.php文件上传出错

Drag事件编辑器实现拖拽上传图片效果

以上就是keystone.js后台编辑器中上传图片的实现方法的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学