Send custom data with dropzone.js on each File Upload

Send custom data with dropzone.js on each File Upload

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Send Custom Data with Dropzone.js on Each File Upload

Dropzone provides a robust mechanism for handling file uploads, but its default configuration often assumes a static payload. When building dynamic applications, you frequently encounter scenarios where the server-side logic needs context that isn't available in the URL or standard form fields. For instance, in the CodeIgniter project described in recent discussions, developers need to send specific, changing data—like a customer folder name or a temporary token—alongside every file being uploaded.

The standard params option in Dropzone is useful for static metadata, such as CSRF tokens or constant identifiers. However, it falls short when the payload must react to user interaction within the UI. To solve this, you need to leverage the internal event system to dynamically construct the request before transmission occurs.

Intercepting File Creation

The core of a dynamic upload lies in the addedfile event. This event fires immediately after a file is added to the queue but before the actual AJAX request is sent. By listening for this event, you can inspect the file object and modify the request configuration accordingly.

Here is how you would initialize Dropzone with dynamic capabilities:

Dropzone.autoDiscover = false;

const uploadUrl = "<?php echo BASE_URL.'index.php/admin/mail_actions/uploadFile'; ?>";

const myDropzone = new Dropzone("#attachment", {
    url: uploadUrl,
    addRemoveLinks: true,
    dictDefaultMessage: "Click / Drop here to upload files",
    maxFiles: 3,
    maxFilesize: 8,
    
    // Listen for the file being added to the queue
    addedfile: function(file) {
        // Define your dynamic data here
        const customerFolder = $('#toValue').val(); 
        
        // Create a custom params object based on current state
        const customParams = {
            customerFolder: customerFolder,
            userId: getCurrentUserId() // Example of fetching another dynamic value
        };

        // Modify the file's formData to include our custom data
        // We use setFormData to inject properties that will be sent with the request
        this.options.addFile.bind(this)(file, {
            params: customParams
        });
    }
});

In this implementation, we are injecting dynamic values into the params object specifically for the current file. The this.options.addFile.bind(this) method allows us to trigger the upload logic with our modified parameters attached to the specific file instance. This ensures that when Dropzone sends the AJAX request, it carries the unique data required for that specific upload session.

Handling Cleanup Dynamically

The original snippet also included a removedfile handler intended to delete files from the server upon removal. While functional, there is a subtle distinction between "removing" a file from the UI (via the remove link) and deleting it from the backend storage.

If your requirement involves sending a dynamic ID to delete a specific file version, you must ensure the id sent in the AJAX call corresponds to a unique identifier generated by the server upon successful upload, not just the client-side filename. Storing only the file name as an ID (data: "id="+fileName) is generally discouraged if multiple files share similar names or if duplicates are allowed.

A more robust approach would be to retrieve the server-assigned unique ID after the upload completes successfully via the success event, and then use that specific ID in your cleanup logic:

addedfile: function(file) {
    // ... setup dynamic params as shown above ...
},

success: function(event) {
    const response = JSON.parse(event.target.responseText);
    
    // Assuming the server returns a unique file_id in the JSON response
    if (response.success && response.fileId) {
        this.files[file.fileIndex].file_id = response.fileId;
        
        // Now your removedfile handler can use this unique ID
        this.on('removedfile', function(file) {
            $.ajax({
                type: 'POST',
                url: "<?php echo BASE_URL.'index.php/admin/mail_actions/deleteFile'; ?>",
                data: "id=" + file.file_id, // Use the stored unique ID
                dataType: 'html'
            });
        });
    }
}

Implementation Considerations

When implementing dynamic parameters, always verify that your server-side controller correctly processes the incoming FormData object. In CodeIgniter or Laravel, accessing these data points usually involves reading from the superglobal array (like $this->input->post() or $request->input()) rather than expecting query parameters.

Additionally, be mindful of rate limiting and concurrent uploads. If you are dynamically generating tokens or unique identifiers for every file in the queue, ensure your backend is prepared to handle multiple simultaneous requests with different contexts without collision.

For developers working within the Laravel ecosystem, similar dynamic behavior can be achieved using Laravel\Passport or custom middleware to validate these incoming dynamic parameters before processing the storage logic. If you are looking for deeper integration patterns between file managers and frameworks like this, resources often discussed by community experts at https://laravelcompany.com highlight best practices for securing file uploads in modern web stacks.

Our network