使用TinyMCE上传图片到服务器

3

上传处理程序

我打算在服务器端为tinymce编写一个上传处理程序。经过很多搜索,我发现了这个使用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' });
    });
  }
});

我需要检查上传的文件是否真的是图像文件,或者只是服务器上类似于图像文件的扩展名! - Saeed
1个回答

2

我在服务器端添加了以下代码,文件已成功上传:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接