Uploading XML-file...

Uploading XML-file...

ztevieztevie Posts: 101Questions: 23Answers: 5

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

  • allanallan Posts: 63,834Questions: 1Answers: 10,518 Site admin
    Answer ✓

    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

  • ztevieztevie Posts: 101Questions: 23Answers: 5

    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.

  • ztevieztevie Posts: 101Questions: 23Answers: 5
    edited December 2016

    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:

    Field::inst( 'blasts.file' )
                ->setFormatter( 'Format::ifEmpty', null )
                ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/blastfiles/__ID__.__EXTN__' )
                    ->db( 'files', 'id', array(
                        'filename' => Upload::DB_FILE_NAME,
                        'web_path' => Upload::DB_WEB_PATH,
                        'system_path' => Upload::DB_SYSTEM_PATH
                    ) )
                    ->dbClean( function ( $data ) {
                    // Remove the files from the file system
                        for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                            unlink( $data[$i]['system_path'] );
                        }
                        // Have Editor remove the rows from the database
                        return true;
                    } )
                    ->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.file",
                    type: "upload",
                    display: function ( file_id ) {
                        //return editor.file( 'files', file_id ).web_path;
                        //return '<span>' + editor.file( 'files', id ).filename + '</span>';
                        return editorCreate.file( 'files', file_id ).filename;
                    },
                    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"
                }
            ]
        } );
    
  • allanallan Posts: 63,834Questions: 1Answers: 10,518 Site admin

    One thing I wonder with the line editor.file('files', file_id).web_path? What is .file referring to?

    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

  • ztevieztevie Posts: 101Questions: 23Answers: 5
    edited December 2016

    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.

  • allanallan Posts: 63,834Questions: 1Answers: 10,518 Site admin

    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

  • ztevieztevie Posts: 101Questions: 23Answers: 5
    edited December 2016

    Oh, it seems to be 1.5.6.

    Is it safe to download and use 1.6 then? No major changes that will make me have to rewrite code I suppose?
    EDIT: Never mind, I saw the release notes... Great!

    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:

    editorCreate = new $.fn.dataTable.Editor( {
            ajax: "../_includes/process_adminblasts.php",
            table: "#tbl-admin-blasts",
            fields: [ {
                    label: "Salva:",
                    name: "blasts.file",
                    type: "upload",
                    display: function ( file_id ) {
                        //return editor.file( 'files', file_id ).web_path;
                        //return '<span>' + editor.file( 'files', id ).filename + '</span>';
                        $('#DTE_Field_testing').val(eTable.file( 'files', file_id ).web_path);
                        return eTable.file( 'files', file_id ).filename;
                    },
                    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"
                }, {
                    label: "",
                    name:  "testing",
                    type: "hidden"
                }
            ]
        } );
    
  • ztevieztevie Posts: 101Questions: 23Answers: 5

    Maybe "hidden" here also makes it "disabled" for input?
    I did try to show (), then set the value and then hide (). But no go...

  • allanallan Posts: 63,834Questions: 1Answers: 10,518 Site admin
    Answer ✓

    If I use a hidden field in the editor I can't change it's value.

    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() or field().val().

    Allan

This discussion has been closed.