Editor node.js file upload example error - A server error occurred while uploading the file

僤鯓⒐⒋嵵緔 提交于 2019-12-25 01:49:18

问题


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

In the frontend I'm getting:

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
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"
      }
});

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`);   

回答1:


I forgot to add 'express-busboy' which solved the issue ...

let bb = require( 'express-busboy' );

bb.extend( app, {
    upload: true
} );


来源:https://stackoverflow.com/questions/57075113/editor-node-js-file-upload-example-error-a-server-error-occurred-while-uploadi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!