About Ajax Image Upload

A proof-of-concept helper to implement frontend Ajax Image Upload.

Category Proof of Concept
Proof of concept modules are designed as examples or starting points for others to build from. May not be ideal for users wanting to plug-n-play.
Release StateAlpha
Non-stable. Not yet intended for use in production environments. *
Authorboundaryfunctions
Module Version0.0.1
Class NameAjaxImageUpload
Compatibility2.3, 2.4
Date AddedNovember 10, 2013
Recommended ByNew recommendations may take up to 1 day to appear.

Instructions

This module's files should be placed in /site/modules/AjaxImageUpload/
How to install or uninstall modules

README

Ajax image upload helper for Processwire 2.x

Introduction

This module is more a proof-of-concept than a stable drop-in and it's in a really early stage with an ugly API. I still decided to publish it because it maybe includes some valuable helpers how an Ajax file upload in Processwire can be done.

Installation

Copy the files into your site/modules directory and install in the backend as you're used to.This will automatically add the needed (system) templates and a new page called "Ajax Image Upload" in your pages tree. Newly uploaded images will be stored with pages that are children of this page.

Usage

You would want to use this inside a form that ultimatively creates a new page with an image uploaded via Ajax.In the template of the form page you will have to add the following:

$ajaxImageUpload = $modules->get('AjaxImageUpload');
$ajaxImageUpload->addScripts();

This will make the module available in your template as $ajaxImageUpload and add the jQuery File Upload script to $config->scripts (you are still obliged to include jQuery and actually load the scripts in $config->scripts on your own).

At some point you should then output a form field to hold your image:

<form id="my_form">
    <label for="my_img">Image</label>
    <span class="button-image-upload">
        <span class="button-image-upload-label">Bilddatei auswählen...</span>
        <input type="file" id="my_img" name="my_img" data-url="<?php echo $config->urls->root . $ajaxImageUpload->handlerUrl; ?>" />
    </span>
</form>

You will also have to set up jQuery File Upload as documented in your JS on the page:

var $myForm = $('#my_form'),
  $myImgField = $myForm.find('#my_img');

$myImgField.fileupload({
  dataType: 'json',
  done: function (e, data) {
      if (data.result.hasOwnProperty('pageName') && data.result.pageName) {
        $myForm.find('#my_image_page').remove();
        $('<input />')
          .attr('type', 'hidden')
          .attr('id', 'my_img_page')
          .attr('name', 'my_img_page')
          .val(data.result.pageName)
          .appendTo($myForm);
      }
    }
});

You can use all their nice configuration options and callbacks to make it really do what you want, i.e. show a preview after successful upload, show a progress meter and the likes, but this should do all that is needed.When the upload works as it should, the response should have a variable (pageName) that holds the name of the newly created page that holds your image. It is then added to your form as a hidden input with the name my_img_page.

When you want to use this image on a page, you can make use of the transferImages method of the module. This might look like this:

$targetPage = new Page(); // the page you want to insert the image on
$targetFieldname = 'my_img_field'; // the field into which you want to insert the image

if ($input->post->my_img_page) {
    /* Sanitize the upload page name */
    $myImgPageName = $sanitizer->name($input->post->my_img_page);

    /* Get the upload page */
    $myImgPage = $pages->get("name=$myImgPageName,template=ajax_image_upload");

    if ($myImgPage && !($myImgPage instanceof NullPage)) {
        /* Transfer image from upload page to target page */
        $ajaxImageUpload->transferImages($myImgPage, $targetPage, $targetFieldname);
    }
    /* At some point save the target page */
    $targetPage->save();
}

At this point, your image uploaded via ajax should be part of the target page.

To do

  • A much, much, much cleaner and DRYer API
  • Ready to use client-side code
  • Proper documentation

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.