ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Safecracker Ajax File Uploads

July 19, 2013 8:06am

Subscribe [1]
  • #1 / Jul 19, 2013 8:06am

    box-head

    177 posts

    Hello,

    I have a Safecracker form set up and running and I’d like to change the way that file uploads work so that they are submitted and saved to the server when a user selects them via ajax (so that I can set a preview thumbnail and show a progress bar), rather than sending all the files at once to the server when the form submits and losing the UX features.

    I have the ajax functionality working, saving the file to the correct server directory, and I have it returning the filename and extension. I use these to fill a hidden custom field value for example

    <input type="hidden" name="custom_image_field" value="filename.jpg">

    ‘custom_image_field’ is set up as a Safecracker File field.

    When I submit the Safecracker form I notice that the ‘custom_image_field’ is set in the $_POST array and that Safecracker correctly processes the right field_id_X and assigns the filename to that too.

    However having traced through the Safecracker library I noticed that after the form validation is run the field_id_X key in $_POST is returned empty, and so no filename is saved to the entry record, and no record is saved under the exp_files table either.

    My presumption is that because no $_FILES array is sent to the server that any file fields are just reset to empty following the form validation.

    Question 1: Is there a way around this?

    Next Steps:
    Knowing that the file exists on the server following the ajax upload, I looked at setting the ‘custom_image_field_hidden’ input field value to the returned filename. Similar to how Safecracker would output the field on an edit entry page. Like this.

    <input type="hidden" name="custom_image_field" value="NULL">
    <input type="hidden" name="custom_image_field_hidden" value="filename.jpg">

    When submitting the form, this successfully writes the filename (and file directory prefix - { filedir_1 }) to the entry record.

    This does unearth another problem however in that no record is set in the exp_files table and so the exp:channel:entries loop isn’t able to locate the image data.

    Question 2: Is it possible to synchronize a file directory via an API call?

    OR

    Question 3: Is it possible to create a new record in the exp_files table via an API call?

    OR

    Question 4: Would manually creating the exp_files record (longhand SQL INSERT query) work?

    Question 5: Would any of these approaches fix the problem and allow the channel entries loop to output the ‘custom_image_field’ data?


    We’re running EE 2.6.1

    Thank you for your time and any suggestions.
    Bh

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases