Editor - Upload file (Handling aJax)
Editor - Upload file (Handling aJax)
ZakInterDev
Posts: 51Questions: 16Answers: 0
Hi there
As the title suggests, I'm trying to upload a file and handle the aJax response myself. Following this https://editor.datatables.net/reference/field/upload guide, I see it gives the option to specify my ajax option. Also tried with a jQuery ajax object, but no luck even hitting the URL there
I've currently got
{
label: "CSV:",
name: "bulk_csv",
type: "upload",
ajax: {
type: "POST",
url: "/bulk_upload",
done: function (json) {
//Make your callback here.
console.log(json);
}
}
}
as a field in my Editor instance.
Any ideas, or is it even possible?
Thanks in advance
This discussion has been closed.
Answers
Yes it is quite possible and what you have above looks like it should actually work. Are you getting any errors shown in your browser's console? Are you using 1.5.5?
Do you have a link to the page so I can debug it live please?
Allan
Getting a console error
Using Editor v1.5.1 is there a bug in this version that I'm unaware of?
I'll look into a test page, if the above doesn't help out
Just upgraded to v1.5.5, no longer getting the console error but i'm getting a "A server error occurred while uploading the file" as a DataTables error. I assume this is a default response since I'm trying to override it.
The console log in my 'done' function doesn't appear either.
After a bit further investigation into dataTables.editor.js I see the "Editor.upload" function has it's own success & error handling functions....and looks like these aren't overwritten by my one (If I change "done" to "success" for example)
Edit:
I can confirm by commenting out this code in the dataTables.editor.js file
I was able to use my success function
Would this be something you'd be looking into changing, over writing the callbacks of success & error
Hi,
Thanks for your investigation. I had thought that your
done
would actually be executed since I don't use that in Editor - it sounds like that is not the case.The problem with allowing
success
to be overwritten is that you need to provide all of the same logic as it implements. Do you actually need to do that, or do you just want to be informed when an upload is complete?If the latter, then listen for the
upload.editor
event of theupload
field type. That will let you know when the upload is done.Allan
Yeah the
done
function didn't execute.I don't personally need it. I'm using this upload feature to upload a bunch of barcodes, validate them and return a json list for select2. On the success, I automatically tag them into the list for the user. So they don't have to scan them individually.
The problem with listening for the event, is having to respond with what your
success
function is expecting. I think it would be better for us to over write it if we wish, or somehow add to the function...(not sure if this is possible, I don't know all of Javascripts capabilities). Unless you can provide the JSON that is expected, similar to how you've done on https://editor.datatables.net/manual/serverIt looks to be like just the ID is required, but I'd like to confirm with you. Also I don't really need to keep this file I'm uploading, as it's just used to populate the select2 field. So going back to what I've said previously, I'd prefer to override the
success
function :)Thanks
The point of the event is that you don't need to respond to the success function at all - its already done for you. If you replaced the success function then that is where I think it would get messy.
Good point - I really need to add that to the documentation! In the meantime, this is the structure:
where:
TABLENAME
is the table name used to store the file information in the dbID
is the file idDATAOBJECT
is the information for the file from the dbThe
ID
in theupload
object should be the id of the newly uploaded file. All files in the table must currently be returned.Allan
Ok, I'll give it ago! Sounds like it should work. Will post my solution here if it works, in case anyone else is planning on doing the same. Unfortunately other work has popped up, but will try get around to it asap. Thanks for the JSON :)
Ok so few problems. First off this is the JSON I'm now returning.
I just threw in temp data, as the actual file getting uploaded doesn't matter. As I'm just trying to fool the uploads
success
function into thinking everything is A-OKAY! ...but that's failing as I'm still getting the errorA server error occurred while uploading the file
I also noticed the event
'upload.editor'
only has the event object and value (Primary key) passed to it. This wouldn't work for me as I'm returning a list for my select2 field. I'd need to access all of the JSON returned by the server.Any thoughts? :)
p.s.
I'm assuming my "DATAOBJECT" as correct, as per https://editor.datatables.net/manual/php/upload
p.p.s.
I put some console logs in. One in the event, one before the ajax starts in
dataTables.editor.js
and another in thesuccess
function. They also execute in that order. So I feel it would be impossible to get the JSON response?That suggests that invalid JSON was returned. If you use your browser's network inspector tools, what exactly was returned?
Your DATAOBJECT looks good. I presume you have changed TABLENAME and ID to be the table name and the id of the file?
Allan
This is the JSON being returned. I haven't changed those keys as I don't actually have a table to store this data, because it doesn't need storing. The file being uploaded is simply used to fill the select2 field, making the file useless once this has been done. So I assume these keys/fields can be dummy data?
To give you more of an inside look into what I'm trying to do. This is my serverside function, handling the upload.
So I'm looking to return the
$select2_arr
variable....but first I'm just trying to get this to work without adding that into the JSON