Grab and drop into place

I have been searching for solutions, but so far I haven't found anything that works for my specific case. I have an upload button that currently works on a click event, but I also want it to be able to drag and drop files for uploading.

Here is the HTML code snippet:

        <div style="margin-top: 0px; width: 45%"  ng-model="files" class="drop-box" onclick="document.getElementById('step-image-upload').click()" ng-disabled="instruction.locked || uploading">
            <p>Drop image files<br/></br/>or click to upload</p>
        </div>

Javascript and Angular code:

scope.fileSelected = function(files)
    {
        var promises = [];
        $scope.uploading = true;

        uploadService.uploadMultiple(files)
        .then(function(images)
        {
            if(!images)
            {
                return $q.reject();
            }

            images.forEach(function(image)
            {
                image.highlightAreas = [];
                promises.push($scope.selected.step.addImage(image)
                .then(function()
                {
                    return $q.resolve(image);
                }));
            });

            return $q.all(promises);
        })
        .then(function(images)
        {
            var promises = [];
            var newImageNumber = $scope.selected.step.images.length;

            images.forEach(function(image, index)
            {
                promises.push(ImageExtension.createLinked({
                    imageNumber: newImageNumber + index,
                    caption: ""
                },
                image)
                .then(function(imageExtension)
                {
                    image.imageExtension = imageExtension;
                    return $q.resolve(true);
                }));
            });

            return $q.all(promises);
        })
        .then(function()
        {
            $scope.uploading = false;
        })
        .catch(function()
        {
            $scope.uploading = false;
            toastService.toastTranslation("CLIENT.EDIT.STEP.UPLOAD_ERROR");
        });

    };

Note: My main goal is to enable dragging and dropping of files to trigger the upload functionality, utilizing the existing file handling function.

Answer №1

For anyone facing a similar issue, here is how I resolved my problem: I utilized the ng-file-upload module (https://www.npmjs.com/package/ng-file-upload) HTML:

<div ngf-drop="dragDrop($file)" ngf-select="fileSelected($files)" ng-model="file" class="drop-box" ngf-drag-over-class="'dragover'" ngf-multiple="true" ngf-allow-dir="true" accept="image/*,application/pdf" ngf-pattern="'image/*,application/pdf'" ngf-drop="upload($file)" style="width:45%; cursor: pointer">

The ngf-drop="dragDrop($file)" function triggers drag and drop functionality when a file is dropped into the box. The ngf-select="fileSelected($files)" function on click allows you to search through your folders to upload a file.

Javascript:

    $scope.dragDrop = function(file) 
    {
        var files = [
            file
        ];
}

and

    $scope.fileSelected = function(file)
    {
         var files = [file];
}

I hope this solution proves helpful for someone else in need.

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

Combining an Image with a CanvasJS Graph and Generating a Downloadable Image of the Composite

I am attempting to combine an image (a background image for my graph) with my canvasJS chart. Once these elements have been merged on a canvas, I aim to obtain a DataURL of this canvas, enabling me to download an image of it (depicting the graph along wit ...

Sending state data in an Axios POST request

I'm trying to include a state in an axios post request to send data to my backend, but I'm getting an error saying the state I selected is not defined. Here's the code snippet: React frontend import React, { Component } from "react&qu ...

Modifications performed on the Xhtml generated from xml and XSLT should be mirrored back into the original XML document

After transforming an XML file with xsl and loading it into a browser as html, I am making the html editable using the content editable attribute of html5. How can I transform their edits back to the original xml document, even if they add new nodes to e ...

Update the current value in array.prototype

Recently, I've noticed that the built-in JavaScript sort function can be unreliable at times. To address this issue, I decided to create my own sorting function. Consider the following scenario: Array.prototype.customSort = function(sortFunction, upd ...

Instructions on how to export an HTML table to Excel or PDF by including specific buttons using PHP or JavaScript

I created a table to display leave details of employees with sorting options from date to date. Now, I need to include buttons for exporting the data to Excel and PDF formats. Check out the code below: <form name="filter" method="POST"> <in ...

How can I showcase a chosen name in a <p> tag using a Select dropdown, and then pass the selected ID to a controller in AngularJS

I am working with a select input in my HTML that is populated using ng-options. My goal is to display the selected NAME below, while sending the selected ID back to the controller. I am able to access the required id from ng-model="user.category". How ca ...

Unit testing in Angular JS is crucial, especially when testing functions in services that do not return any values

Apologies if this has been asked before, but I couldn't find a solution to my issue. I need to create tests for a service within an Angular JS application. The main function of the service returns and is used as an external method. There are also a ...

The pagination in Laravel Vue is causing a validation error due to an invalid prop type check failing

Recently, I delved into working with Nuxt.js and decided to install the Laravel-Vue-Pagination plugin. However, I encountered an error message in my console system that reads: [Vue warn]: Invalid prop: type check failed for prop "data". Expected Object, ...

Using several ng-repeats in a single element

Is it possible to create a code structure similar to this example: <tr ng-repeat="data in dataArray, value in valueArray"> {{data}} {{value}} </tr> I have two arrays that I would like to display on the same row. PS: I'm not see ...

Dealing With HttpClient and Asynchronous Functionality in Angular

I've been pondering this issue all day. I have a button that should withdraw a student from a class, which is straightforward. However, it should also check the database for a waiting list for that class and enroll the next person if there is any. In ...

The Google Apps spreadsheet script occasionally fails to complete iteration but functions properly in all other aspects

I have a script that goes through a spreadsheet of student data one row at a time to email students and their parents if the student's grade is below 60. The spreadsheet columns include: Student ID, Name, Email Address, Parent's Email Address, an ...

Eliminating the need for RequireJS in the Typescript Visual Studio project template

After integrating RequireJS into my Typescript template using the nuget package manager, I found that it was more than what I needed and decided to uninstall it. Even though I removed the package through nuget and the files were deleted properly, my Typesc ...

What is the best way to eliminate blank values ("") from an array?

I am working with a two-dimensional array that was generated from an HTML table using jQuery, but I have noticed that some values are empty and are displaying as "". How can I go about removing these empty values from the array? <table> ...

Asynchronous operations in Node.js with Express

Hey, I'm still pretty new to using async functions and I could really use some help understanding how to pass callbacks in async.each for each item to the next level (middleware). Here's the logic: I want to iterate through all items, and if an ...

Utilizing AngularJS HotTowel to trigger a spinner when making AJAX requests

I am attempting to trigger the spinner whenever there is an xhr call within the application. Currently, the spinner only appears when clicking on a menu or navigating to a different page. Index page <aside class="main-sidebar"> <!-- ...

What is the best way to transfer the value of one directive attribute to another directive in AngularJS?

For instance: <custom-main> <custom-sub1 att-name="test"></custom-sub1> <custom-sub2></custom-sub2> </custom-main> JavaScript Source Code bosAppModule.directive('custom-main',[&apos ...

Angular 2 has its own version of $q.when called RxJs

Back in the AngularJS 1.* days, I used to have this code snippet to refresh the auth-token: ... if (!refreshTokenInProgress) { refreshTokenInProgress = AuthService.refreshToken(); } $q.when(refreshTokenInProgress, function () { refreshTokenInProgre ...

Implement an event listener to detect incoming SMS messages using the mozMobileMessage API within the Boot to Gecko (B

As part of a university assignment, I have embarked on the journey of learning how to develop apps using AngularJS for Firefox OS. This marks my maiden voyage into the realm of JS app development. The primary objective of this app is to execute commands t ...

Implementing an asynchronous task queue in AngularJS

I came across this JSFiddle example where three consecutive calls are made to a service function, simulating $http Request interceptor function which returns a promise. The current code does not wait for the previous call to finish before making the next o ...

I am looking to dynamically load a script only after retrieving specific data from a JSON file in Next.js

I am trying to ensure that the Script tag loads after the data.post.content is loaded within the HTML. Specifically, my goal is to execute the MathJax.js script inside the HTML. This is the code I have: return ( <div> <h1>{data.post ...