Customizing File Size and Dimensions in Form Submission with Dropzone.js in JavaScript

I am currently experimenting with some sample code for Dropzone.js and am interested in finding a way to include the file size as a form field when submitting:

var KTFormsDropzoneJSDemos = {
    init: function(e) {
        new Dropzone("#kt_dropzonejs_example_1", {
                url: "add/submit/",
                paramName: "file",
                maxFiles: 10,
                maxFilesize: 10,
                addRemoveLinks: !0
            }),
            function() {
                const e = "#kt_dropzonejs_example_3",
                    o = document.querySelector(e);
                var r = o.querySelector(".dropzone-item");
                r.id = "";
                var t = r.parentNode.innerHTML;
                r.parentNode.removeChild(r);
                var l = new Dropzone(e, {
                    url: "add/submit/",
                    parallelUploads: 20,
                    maxFilesize: 0.25, // 1 MB
                    acceptedFiles: ".jpeg,.jpg",
                    previewTemplate: t,
                    previewsContainer: e + " .dropzone-items",
                    clickable: e + " .dropzone-select"
                });
                l.on("addedfile", (function(e) {
                    o.querySelectorAll(".dropzone-item").forEach((e => {
                        e.style.display = ""
                    }))
                })), l.on("totaluploadprogress", (function(e) {
                    o.querySelectorAll(".progress-bar").forEach((o => {
                        o.style.width = e + "%"
                    }))
                })), l.on("sending", (function(e) {
                    o.querySelectorAll(".progress-bar").forEach((e => {
                        e.style.opacity = "1"
                    }))
                })), l.on("complete", (function(e) {
                    const r = o.querySelectorAll(".dz-complete");
                    setTimeout((function() {
                        r.forEach((e => {
                            e.querySelector(".progress-bar").style.opacity = "0", e.querySelector(".progress").style.opacity = "0"
                        }))
                    }), 300)
                }))
            }()
    }
};
KTUtil.onDOMContentLoaded((function() {
    KTFormsDropzoneJSDemos.init()
}));

The Dropzone.js "Tips & Tricks" page provides an example of how to send additional data like file size, height, and width:

Dropzone includes data within the file object that can be accessed during events firing. You can retrieve file.width and file.height if it's an image,

If you need to attach extra data specific to each file being uploaded, you can utilize the sending event:

myDropzone.on("sending", function(file, xhr, formData) {
  // Adds the filesize as POST data along with the file.
  formData.append("filesize", file.size);
});

In the provided example code under the #kt_dropzonejs_example_3, I have the "sending" section mentioned but am unsure how to implement adding the file size as form data similar to the example code. Any guidance on modifying the "sending" function to achieve this would be appreciated.

Answer №1

When handling the sending event based on the example, it is recommended to ensure acceptance of other parameters:

listener.on("sending", (function(file, xhr, formData) {
    container.querySelectorAll(".progress-bar").forEach((element => {
        element.style.opacity = "1"
    }));
    formData.append("filesize", file.size);
})),

In terms of formatting, I suggest a code structure similar to the following:

sampleDropzone.on("sending", (file, xhr, formData) => {
    sampleContainer.querySelectorAll(".progress-bar").forEach(item => item.style.opacity = "1");
    formData.append("filesize", file.size);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The React page loads before verifying the clients' permissions

In my application, I am using a Javascript query to fetch the data of the current user. This data is then used to verify the user's access permissions for different pages in my React app with the help of Apollo Client and GraphQL. Unfortunately, ther ...

What is the best way to conceal an element in jQuery by utilizing a JavaScript variable?

I have encountered a challenge in concealing this specific page element in the provided html code <table cellspacing=5 cellpadding=3> <tr> <td id="lst2"><h4>Apple Ipad for sale N70,000 (negotiable)</h4></td> & ...

Tips for enabling or disabling elements within an array using React JS

I am looking to develop a feature where I can toggle individual boxes on and off by clicking on them. Currently, only one box at a time can be activated (displayed in green), but I want the ability to control each box independently without affecting the ot ...

Can you point me in the direction of the Monaco editor autocomplete feature?

While developing PromQL language support for monaco-editor, I discovered that the languages definitions can be found in this repository: https://github.com/microsoft/monaco-languages However, I am struggling to locate where the autocompletion definitions ...

utilizing async/await without the need for babel-polyfill

Here is the code I am working with: @action async login(payload){ try { this.loginLoading = true const data = await request('/admin/login', { method: 'post', data: payload }) this.logined = ...

Oops! There was an issue trying to solve the command "/bin/bash -ol pipefail -c npm run build" while deploying a Next.js app on Railway. Unfortunately, it did not complete successfully and returned an exit code of

[stage-0 8/10] RUN --mount=type=cache,id=s/8a557944-4c41-4e06-8de9-06bfcc5e8aaf-next/cache,target=/app/.next/cache --mount=type=cache,id=s/8a557944-4c41-4e06-8de9-06bfcc5e8aaf-node_modules/cache,target=/app/node_modules/.cache npm run build: 17.62 17.62 ...

Tips for eliminating unnecessary data in written content

Could anyone provide a recommended method for removing unnecessary symbols from text strings? For instance, transforming "CWC%20-%20Maint%20Eng%20-%20El" into the more readable format of "CWC - Maint Eng - El". ...

Create a compressed package of a Vue project that can easily be inserted into a Blogger blog post as a single HTML file

Is there a way to package all the files related to a Vue.js project (HTML, JavaScript, CSS) into one single HTML file for easy deployment on a Blogger Blogspot post? In the past, a question similar to this was asked regarding bundling files into a single ...

Modify mouse pointer when an object is clicked using JavaScript

Greetings, I am in the process of designing a website for a client. I have encountered a challenge in changing the cursor icon when a user performs a mousedown on an object. There is an image on the webpage When the user clicks on the image, the cursor s ...

Is it possible to leverage specific client-side Javascript APIs on the server-side?

Exploring APIs designed for web browsers that require their .js code to return audio streams. In a broader sense, these APIs provide byte streams (such as audio) for playback in the browser. Is it possible to use these APIs in server-side Javascript frame ...

inserting a dynamic variable into a JSON string

My goal is to create a javascript object, var systemName = {"system" : varA}; However, I want the object to be structured like `{"system" :"varA"} where varA contains the variable value but is enclosed in double quotes. I attempted {"system" : "'+ ...

ReactJS is encountering a situation where two children are using the same key and causing

When I try to view the profile information of another user, I encounter a duplicate key error that says: "Warning: Encountered two children with the same key, ``. Keys should be unique so that components maintain their identity across updates. Non-unique k ...

What is the best way to choose an Animatedmodal that can showcase various demos while using just one ID?

I am currently customizing my website and utilizing the AnimatedModal.js framework. I have successfully displayed content in one modal, but encountered difficulty when attempting to create multiple modals as they all share the same ID. My question is, how ...

Accordion elements that are active will move all other content on the page

I am currently working on creating an accordion using the following code: https://codepen.io/rafaelmollad/pen/JjRZbeW. However, I have encountered a problem where when clicking on one of the accordion items, the content expands and pushes the title upward. ...

Transferring an array of objects from one array to another with the click of a button

I'm facing an issue with moving data between two arrays of objects using buttons in a Nextjs project. The functionality works correctly when selecting a single data item, but it gives unexpected results when selecting multiple items. Although my code ...

Using AngularJS and D3 to create a custom directive that allows for multiple instances of D3 loading within Angular applications

After creating an angular directive for a d3 forced-directed graph and using the code provided here, I encountered some issues with multiple loads. The directive seemed to load six times each time it was initialized, causing performance problems. To addres ...

How can I check if the VPN is turned off in a React application?

import React from "react"; import { Offline, Online } from "react-detect-offline"; export default function DropDown() { return ( <> <Online>Only displayed when connected to the internet</Online> <Offline ...

Transferring information between pages through ajax communication

I am working with two pages named testing.php and submission.php. My goal is to send data from testing.php to be displayed on submission.php. For example, when a user clicks on test1, they should be directed to submission.php where I want to display the te ...

Issue with Pagination functionality when using Material-UI component is causing unexpected behavior

My database retrieves data based on the page number and rows per page criteria: const { data: { customerData: recent = null } = {} } = useQuery< .... //removed to de-clutter >(CD_QUERY, { variables: { input: { page: page, perPag ...

AngularJS special feature: enhance controllers and views by adding notification capabilities

What is the most efficient method for integrating common notification features into necessary controllers in AngularJS? The objective is to establish local notifications that can be effortlessly included or removed from any controller. Key factors includ ...