file upload

file upload

crush123crush123 Posts: 417Questions: 126Answers: 18

i have created 2 pages with this feature ;-)

just an observation, and wondered if I was doing things incorrectly.

on my editor form, (create or edit), once i choose a file, it appears to be uploaded to the server immediately, before i submit the form, so if i cancel the create or edit, the file has been uploaded already.

is this expected behaviour ?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Yes that is the case. The file is uploaded asynchronously from the rest of the form. This is, in part, required by how the async upload works using the local file reader.

    One feature I am planning on adding in future is a "vacuum" whereby the vacuum (i.e. clean) script will run periodically and find all files which are not referenced in the database. This would also resolve the issue of deleting files that we discussed before - although the same issue also arises - it can be potentially quite difficult to implement since the Upload class really allows you to define how the files are defined and stored, rather than imposing a rigid structure.

    Regards,
    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited April 2015

    Thanks Allan,

    In that case, how easy is it to implement a 'processing' event, for an upload, which will work in the same way as when the datatable is loading or the editor is updating.

    Its just that files of say 500k may take a few seconds to upload, and a visual cue would be of benefit

    re the 'delete'. for now i am using the id returned from the delete instance,
    if i get a row returned from my select statement i pull out the file name. if it exists then delete it.

    though i think returning data from the delete, as you mentioned, is the way to go, perhaps make it optional if it isn't needed in most cases

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Its just that files of say 500k may take a few seconds to upload, and a visual cue would be of benefit

    If you have the drag / drop option enabled it should show percentage complete upload text while it is uploading.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    Okay, I will look at that.

    As an aside, is it possible to capture any data from the uploaded file if the editor form is not posted ?

    I am thinking that if I could recover the file name, I could check the database table to see if an entry exists for it, and delete the file it if it doesn't

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You could get the value from the field possibly and then send an Ajax request to the server to do the tidy up. Perhaps using the close() event will do it although you need to make sure that it hasn't been posted to the server!

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited April 2015

    "although you need to make sure that it hasn't been posted to the server!"

    I have something which appears to work, (without the check you mentioned), but I am at the limits of my knowledge here, so appreciate the input.

    I retrieve the value of the file field on close and pass it into my ajax as a parameter.
    My ajax page passes this value as a filter into my recordset.
    If the recordset returns any rows, i know the table references the file, so leave it alone, otherwise I delete it

    editor.on( 'close', function () {
        var val = editor.field( 'tblblog.BlogImageURL' ).val();
        $.ajax ({
                url: '/ajax/ajax_blog_delete_orphan.php?imagename='+val+'',
                dataType: 'json',
                success: alert(val)
            })
    

    } );

    This doesn't allow for orphan files being left on the server if i make an edit, but its a start

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    I think possibly the best thing to do would be to have something alongs the lines of the vacuum I discussed above running periodically, or perhaps even on every data load. Then if there is an orphaned file, it isn't going to be there very long.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    I think that's the best solution.

    I got something working using the glob() function to check the folder for all files with a specific extension(s) and store the names in an array.
    I then looped through a recordset and stored the file names in a second array.

    I then just used array_diff() and looped through this new array to delete the orphans.

    Works for me

This discussion has been closed.