I am using com_ajax in a module. I have a form with text inputs which I can pass through to the method and store to database but I am struggling to upload a file.

Code that works fine for text inputs is as follows


<div id="resultsDiv"></div>

<form name="form1" method="post" action="" id="form1">
    <input type="text" name="text1" id="text1">
    <input type="text" name="text2" id="text2">
    <button type="button" id="btnAdd">Save</button>


// Include the helper.
require_once __DIR__ . '/helper.php';

// Instantiate global document object
$doc = JFactory::getDocument();

$js = <<<JS
(function ($) {

$(document).on('click', '#btnAdd', function () {
    var value   = {text1:$("#text1").val(),text2:$("#text2").val()};
    request = {
            'option' : 'com_ajax',
            'module' : 'ajax_text',
            'method' : 'addRecord',
            'data'   : value,
            'format' : 'raw'
        type   : 'POST',
        data   : request,
        success: function (response) {



require JModuleHelper::getLayoutPath('mod_ajax_test');


public static function addRecordAjax()
    $input = JFactory::getApplication()->input;
    $formData = new JInput($input->get('data', '', 'array')); 
    $text1 = $formData->getString('text1');
    $text2 = $formData->getString('text2');

    $db = JFactory::getDbo();

    // Create a new query object.
    $query = $db->getQuery(true);

    // Insert columns.
    $columns = array(

    // Insert values.
    $values = array(    

    // Prepare the insert query.
        ->values(implode(',', $values));

    // Set the query using our newly populated query object and execute it.
    $result = $db->execute();

    if ($error = $db->getErrorMsg()) {
        throw new Exception($error);


If it is possible to do so how might I go about:

  1. Upload file to directory on server and store location in database

  2. Upload file and store binary data to database blob field

Please provide examples.


  • You didn't mention what you already tried to upload file. The above code is just for form fields. Commented Apr 29, 2015 at 5:21
  • @SD Do you know if it is possible to do with com_ajax? What would you recommend trying?
    – danielrt9
    Commented Apr 29, 2015 at 7:03
  • Have you tried jquery.form.min.js for calling ajax method. It will send files input to url. you can then get it working by handling it with Joomla File API Commented Apr 29, 2015 at 8:57

1 Answer 1


Here is the method I used to upload file using jQuery Form plugin.

First you need to set form enctype to allow file uploads.

<form name="form1" method="post" 
 action="<?php echo JRoute::_('index.php?option=com_myapp&task=mycontroller.upload');?>" 

 <input type="file" name="myFile">

Then use JavaScript to upload on click of some button. for eg.


  // do some preprocessing stuff like validations

  // now upload
    type            : 'POST',
    dataType        : 'json',
    success     : function(data, status, xhr, jqForm){
      //do post upload stuff

In your component controller function. use $app->close() to send raw json response after upload

echo 'Upload Success';
$app = JFactory::getApplication();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.