编辑器node.js文件上传示例错误-上传文件时发生服务器错误 [英] Editor node.js file upload example error - A server error occurred while uploading the file
本文介绍了编辑器node.js文件上传示例错误-上传文件时发生服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编辑器node.js文件上传示例错误-上传文件时发生服务器错误
Editor node.js file upload example error - A server error occurred while uploading the file
我没有运行基本文件上传示例:
I'm not getting the basic file upload example to run:
https://editor.datatables.net / manual / nodejs / upload
在前端,我得到:
A server error occurred while uploading the file
...时发生服务器错误上传一个文件。
... when trying to upload a file.
let {
Editor,
Field,
Validate,
Format,
Options,
Upload,
promisify
} = require('datatables.net-editor-server');
let unlink = promisify(fs.unlink); // await version of unlink
var getBody = JSON.parse(req.body.json);
let editor = new Editor( db, 'news', 'id' )
.fields(
new Field( 'news.id', 'id' ),
new Field( 'news.cruser_id', 'cruser_id' ),
new Field( 'news.image', 'image' ).setFormatter( Format.ifEmpty(null) ).upload(
new Upload( __dirname + '/../public/uploads/{id}.{extn}' )
.db('image', 'id', {
fileName: Upload.Db.FileName,
fileSize: Upload.Db.FileSize,
web_path: '/uploads/{id}.{extn}',
system_path: Upload.Db.SystemPath
})
.validator( Validate.fileSize(500000, 'Files must be smaller than 500K') )
.validator(
Validate.fileExtensions(
['png', 'jpg', 'gif'],
'Only image files can be uploaded (png, jpg and gif)'
)
)
.dbClean(async function(data) {
for (let i = 0, ien = data.length; i < ien; i++) {
await unlink(data[i].system_path);
}
return true;
})
)
)
await editor.process(getBody, req.files);
res.json(editor.data());
我缺少了什么,我该如何解决?
What am I missing and How can I solve this?
js编辑器
// Editor
editor = new $.fn.dataTable.Editor( {
ajax: {
url: "/news/"
},
table: "#news"
fields: [{
label: "Image:",
name: "image",
type: "upload",
display: function ( file_id ) {
return '<img src="'+editor.file( 'image', file_id ).web_path+'"/>';
},
clearText: "Clear"
}
});
表 image
Table structure for table image
CREATE TABLE `image` (
`id` int(11) UNSIGNED NOT NULL,
`fileName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`fileSize` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`web_path` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`system_path` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `image`
ADD PRIMARY KEY (`id`);
推荐答案
我忘了添加 express-busboy解决了问题...
I forgot to add 'express-busboy' which solved the issue ...
let bb = require( 'express-busboy' );
bb.extend( app, {
upload: true
} );
这篇关于编辑器node.js文件上传示例错误-上传文件时发生服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文