Editor node.js file upload example error - A server error occurred while uploading the file
Editor node.js file upload example error - A server error occurred while uploading the file
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?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
js editor
Table structure for table
image
Does the console show anything where you are running the server? Hopefully it should be showing an error message - possibly about file permissions or something else.
Allan
I don't see anything suspicious. Also no debug info if I do editor.debug(true); The XHR is also OK. Yet, there I noticed compared to your example https://editor.datatables.net/examples/advanced/upload.html
that the POST response after choosing a file in your case e.g. shows:
While mine shows the regular response as if I would (as usually) load the table:
... so no files information ... plus the fronend comment: A server error occurred while uploading the file.
The permissioning of the folders is like:
In the NodeJS libraries you need to use
debug: true
in your Knex configuration to have it dump information about the SQL statements out to the console.Allan
There I also don't find anything suspicious cry ...
Editor might be swallowing the error in order to present a nice user error - change your
process()
line to be:Allan
Still nothing. Btw ... shouldn't req.files have any values? It's still undefined even after choosing a file:
When I check the editor ajax:
and submit it without a image I can see in fe console as expected:
But when I upload a picture I get:
And ...
... shows in the front end:
Looks like FormData{} is empty.
Might the problem be that I have two different instances ... one for the backend and another for the frontend?!
It should when you upload a file yes.
Have you got the express-busboy module installed for your node app?
Allan
That was it, many thanks!