上传处理程序
我要在服务器端为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="">
Controller 中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' });
});
}
});
最佳答案
我在服务器端添加这行代码,文件成功上传:
var fileUpload = require('express-fileupload');
var mime = require('mime');
app.use(fileUpload({}));
关于angularjs - 使用tinymce上传图像到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44845597/