【问题标题】:uploading images with tinymce to server使用 tinymce 将图像上传到服务器
【发布时间】:2017-07-02 00:36:14
【问题描述】:

上传处理程序

我将在服务器端为tinemce 编写一个上传处理程序,经过大量搜索,我找到了这个 PHP 示例,

https://www.tinymce.com/docs/advanced/php-upload-handler/

我在 NodeJs 中需要这个处理程序

我的html代码:

<textarea ui-tinymce="X.tinymceOptions" ng-model="X.lessonBody"></textarea>
<input name="image" type="file" id="upload" style="display:none;" onchange="">

在控制器中初始化tinymce:

this.tinymceOptions = {
      plugins: 'advlist autolink lists link image charmap print preview hr anchor pagebreak ' +
      'searchreplace wordcount visualblocks visualchars code fullscreen ' +
      'insertdatetime media nonbreaking save table contextmenu directionality ' +
      'emoticons template paste textcolor colorpicker textpattern imagetools codesample toc',
      toolbar1: 'undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
      toolbar2: 'print preview media | forecolor backcolor emoticons | codesample',
      image_advtab: true,
      image_title: true,
      // enable automatic uploads of images represented by blob or data URIs
      automatic_uploads: true,
      // URL of our upload handler (for more details check: https://www.tinymce.com/docs/configure/file-image-upload/#images_upload_url)
      // here we add custom filepicker only to Image dialog
      file_picker_types: 'image',
      images_upload_url: '/upload',
      file_picker_callback: function (callback, value, meta) {
        if (meta.filetype == 'image') {
          $('#upload').trigger('click');
          $('#upload').on('change', function () {
            var file = this.files[ 0 ];
            var reader = new FileReader();
            reader.onload = function (e) {
              callback(e.target.result, {
                alt: ''
              });
            };
            reader.readAsDataURL(file);
          });
        }
      }
    };

和我在服务器中的请求处理程序(使用 express js):

app.post('/upload', function (req, res) {

  var A= 1;
  var B= 1;
  var C= 1;

  var folderName = path.join(__dirname, '../client/X-' + A);

  if (!fs.existsSync(folderName)) {
    fs.mkdir(folderName, function (err) {
      if (err) {
        console.log(err);
      }
      else {

      }
    });
  }
  else {
    if (!req.files) {
      return res.status(400).send('No files were uploaded.');
    }
    console.log(req.files.file.mimetype);
    console.log(req.files.file.data.byteLength);
    var sampleFile = req.files.file;
    sampleFile.mv(path.join(__dirname, '../', 'client/', 'test.jpg'), function (err) {
      var temp = path.join(__dirname, '../', 'client/', 'test.jpg');
      mime.lookup(path.join(__dirname, '../', 'client/', 'test.jpg'));         // => 'text/plain'
      if (err) {
        return res.status(500).send(err);
      }
      res.send({ 'location': '../test.jpg' });
    });
  }
});

【问题讨论】:

  • 我需要检查上传的文件是否真的是图像文件或者只是扩展名喜欢服务器中的图像文件!

标签: angularjs node.js express tinymce


【解决方案1】:

我在服务器端添加这行代码,文件上传成功:

var fileUpload = require('express-fileupload');
var mime = require('mime');
app.use(fileUpload({}));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2022-09-26
    • 2019-12-21
    • 1970-01-01
    • 2015-12-30
    • 2021-05-20
    • 2019-01-25
    相关资源
    最近更新 更多