Uploading XML-file...
Uploading XML-file...
So I encountered some difficulties while trying to make upload file to work.
What I would need to do is:
1. Choose and upload a XML-file to server.
2. I do not want to insert info about the file to database. Instead I will extraxt certain values from this file and insert those values to database.
3. After this is done the XML-file is not needed anymore.
I've gotten so far as to upload the file to the right location. However it will not return any data back to the client (Maybe this is normal since I think it was meant to return the data inserted into the database back to the client on upload?) except for Upload Object: id: "C:/xampp/htdocs/upload/.xml".
I also noticed Datatables actually reads the xml file and send everything to server when looking at parameters sent from client? It doesn't actually upload the file, just reads the data and make a new file on the server?
As you see the xml is uploaded without the original name, it's just .xml... It is however correct data in that file, the size is excactly 177kB on both places.
To do number 2 above I guess I have to catch(with $_POST['action'] === 'create') the create action before it reaches the Editor part of code, but my main questions:
* Why is the file uploaded without the original name and can I do something about it?
* Any tips how to achieve what I want in 1-3 above?
The code I'm at so far:
Field::inst( 'blasts.blast_name' )
->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )
->allowedExtensions ( array( 'xml' ), "Välj en giltig XML-fil..." )
),
editorCreate = new $.fn.dataTable.Editor( {
ajax: "../_includes/process_adminblasts.php",
table: "#tbl-admin-blasts",
fields: [ {
label: "Salva:",
name: "blasts.blast_name",
type: "upload",
display: function ( file_id ) {
//return editor.file( 'files', file_id ).web_path;
return '<span>jisdicjn jnd</span>';
},
clearText: "Ta bort",
//noImageText: 'No image',
uploadText: 'Välj XML-fil...',
noFileText: 'Ingen fil är vald',
dragDrop: false
}, {
"label": "Tilldelad BM:",
"name": "drillrigs[].rig_id",
"type": "checkbox"
}, {
label: "Status:",
name: "blasts.blaststatus_id",
type: "select"
}
]
} );
This question has accepted answers - jump to:
Answers
Hi,
The upload action needs to return at least the id of the new file, so that it can be stored and referenced. That isn't something that will be very useful in your use case, and to be honest I think that Editor's Upload class doesn't really allow for what you are looking for. That class always assumes that a file will be stored on the server somewhere.
In wonder if instead of using the
upload
field type, it might be better to have a custom field plug-in that will actually read the XML file on the client and send to the server the information read from it (or include that information in the Editor form).Allan
Ok...
I might rethink and save at least id and path and save the file for backup. Could come in handy in a worst case scenario.
Values read from these files will be used in statistics for at least a year after they are uploaded... So even though I save values to DB and will have regular backups, it wont hurt, they are not that big anyway.
Tearing my hair here....
I did create another table in the database that holds info about the uploaded file.
But the problem now is I can't get the "display" to work when choosing a file. Everything works otherwise. The file is uploaded, the database is updated and all that behind the scenes. But I can't display the user have chosen a file, it just quickly blinks and make the upload behind the scenes.
I've looked at your file upload example, but can't understand what's different?
One thing I wonder with the line editor.file('files', file_id).web_path? What is .file referring to? You have no such field anywhere in your example code?
I wnat to display filename: editor.file('files', file_id).filename
If I just try to display file_id above the id of the upload is correct...
No errors returned.
When looking at the answers in Firefox everything looks correct including the above filename.
Here's the code:
It is referring to the
file()
method which is used to get information about a file based on the file id (and also the table name that stores that information).What is the JSON return from the server after the file has been uploaded?
Thanks,
Allan
Ok, solved it. I put the id in a hidden field in the regular table and then called it by table.file('files', file_id).filename
I don't know if it's a acceptable method or if it has any drawbacks?
The json returned looked correct before and looks the same now so it seems editor couldn't find "file" for some reason...
EDIT: Ok, that's weird, I just noticed I had the hidden field in the table commented out from testing stuff, but still it worked with table.file(). So the table has access to file but not the editor, with or without hidden field.
Are you using Editor 1.5.x? The
file()
method is attached to Editor and DataTables in 1.6+ with the documentation focusing on the former as it seems to be easier for people to use. However, as you have seen they are both present and should both have access to the same data.Allan
Oh, it seems to be 1.5.6.
Another question while I'm at it:
If I use a hidden field in the editor I can't change it's value. I'd like to get some values from the upload of the xml-file that will be sent to the server together with the other field values.
If I don't use "hidden" I can change val() without problem... Maybe this is also fixed in 1.6 if I'm lucky?
This below don't work, but take away type hidden on the last field and it works fine. You can see I set the val() in the upload display function:
Maybe "hidden" here also makes it "disabled" for input?
I did try to show (), then set the value and then hide (). But no go...
You can't through the GUI, but you can with the API. Don't try to set field values directly using the DOM or jQuery - use the Editor API methods such as
val()
orfield().val()
.Allan