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. *
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.


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


Ajax image upload helper for Processwire 2.x


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.


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.


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');

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; ?>" />

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');

  dataType: 'json',
  done: function (e, data) {
      if (data.result.hasOwnProperty('pageName') && data.result.pageName) {
        $('<input />')
          .attr('type', 'hidden')
          .attr('id', 'my_img_page')
          .attr('name', 'my_img_page')

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 */

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