Utilizing angularjs to send an image along with additional form fields to a spring rest service

I am looking to create a webpage that allows users to upload an image along with other form fields using AngularJS and Spring REST service. Below is the example of HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.js"></script>
         ... (HTML code continues)
     </head>
      ... (More HTML code)

 <p>Below is the Spring controller method:</p>

<pre><code>@POST
@Path("/uploadImage")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadImage(){

}

If you are curious about how to send files along with data in JavaScript, or how to receive values in the Spring controller, please take a look at the provided examples. They demonstrate how to achieve this functionality using AngularJS and Spring.

Additional code, including controllers and Web.xml configuration, has been included for reference.

Answer №1

If you're working with AngularJS controllers, an example of how to handle image uploads could be:

function uploadImage(){
    var formData = new FormData();
    fd.append('file', multipartFile);
    fd.append('name', name);
    fd.append('description', description);
    fd.append('category', category);
    fd.append('price', price);
    $http.post("yourwebsite.com/uploadImage", formData);
}

For more information on using $http in AngularJS, refer to the AngularJS documentation.

An example of a REST controller that handles image uploads could look like this:

@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
public void uploadImage(@RequestParam("file") MultipartFile file,
    @RequestParam("name") String name, @RequestParam("description") String description, 
    @RequestParam("category") String category, @RequestParam("price") Double price){
    // process the uploaded image (e.g., save it to a database)
}

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

Clearly defined form field even with the inclusion of value=" "

I have a database that I am using to display data in an HTML form for user editing. <input type="text" id="name" placeholder="Name" required = "true" value = <?php if(isset($_SESSION['name'])) echo $_SESSION['name'] ;?>> ...

Package an Angular 1.5.x app that was developed without using npm

When I first built my Angular app, I never imagined it would grow so quickly and become so large. Now I have a whopping ~1.9MB spread across 26 CSS files and 55 JavaScript files! Naturally, this has led to horrendous loading times, with the app taking mo ...

Tips for choosing a text node that is a child of a span node

I am facing an issue with selecting the Active user with tag number 00015339 on my webpage. I am unable to write a correct xpath for it. What I really need is an xpath that can retrieve all Active users regardless of their tag numbers. Below is the code s ...

What should I do when the ng-click event doesn't open a new window

<p ng-click='window.location.href="{{data.url}}"''>{{data.name}}</p> Is there anything incorrect about this code snippet? I attempted using onclick as well, but encountered an error in the console. ...

Adding HTML elements to a button using an array: a step-by-step guide

In the process of developing a web application feature using JavaScript, I have come up with a simple plan: Place a button in the bottom left corner. The button should only become visible after scrolling begins. Upon clicking the button, a new window wil ...

Autocomplete Dropdown Options

I am trying to create a dependent autocomplete drop-down that displays suggestions based on any typed letter, rather than just the first letter. For example, if I type "Yo," it should show "New York" in the dropdown. Below is the code I have been using, ...

Updating a slider based on the values of two other sliders can be achieved by implementing a

I am working on a project that involves three input sliders. I need the third slider to display the product of the values from the first two sliders. Additionally, I want the value of the third slider to update dynamically whenever the values of the first ...

Is there a way to search for the keyword "/test/" within a lengthy string using Regular Expressions?

When faced with a string structured like this: const myString = /example/category/modify?itemID=someID&type=number How can I efficiently extract the segment "/category/" by employing: const subSegment = myString.match(...); My framework of choice i ...

CSS animations make canvas irrelevant

Currently, I am working on a project to develop an interactive whiteboard application using PHP and jQuery. Generating a deck and implementing a canvas overlay for drawing purposes are tasks that I have managed to accomplish without any issues. As a self-t ...

In JavaScript, where are the values saved?

Can you clarify how JavaScript handles storage for primitive types and objects? Are primitive types always stored on the stack and objects on the heap, even within the scope of a function's execution? Also, are globally scoped variables and functions ...

Learn how to implement a conditional class binding in Vue 3, and discover how to apply additional classes when a specific condition is met

As a newcomer to Vue.js 3, I am wondering how to implement a conditional class binding in Vue 3. Specifically, I would like to achieve the following logic: if router.path != '/', then add the class 'nav-bar-two'; otherwise, do not apply ...

Consolidate JavaScript files into a single file using phpStorm

My goal with phpStorm is to consolidate multiple JavaScript files into a single one. To achieve this, I have integrated the closure compiler and set up the file watcher to minify each individual JavaScript file. Now my aim is to merge all of these JavaScr ...

Static Android fragments are necessary

Encountered an error that is causing some trouble, but unable to figure out the solution. Error: Fragments must be static to be re-instantiated by the system, and anonymous classes are not static [ValidFragment] If anyone has insights on how to resolv ...

Passing information from an AngularJS service to a controller

Currently, I am facing a challenge in accessing client-side data through Loopback and the AngularJS SDK. I believe that the issue might be rooted in my lack of experience with Angular, as I am attempting to pass data using an AngularJS controller that invo ...

Error encountered: "Unable to process Three.js FontLoader due to SyntaxError

I attempted to generate 3D text with FontLoader in Three.js, but encountered an error. My Three.js version is r99. const loader = new THREE.FontLoader(); //https://github.com/mrdoob/three.js/tree/dev/examples/fonts loader.load("./fonts/helvetiker_ ...

The Mongo embedded server is functioning as a secondary instance within the replica set, rather than the primary instance

In our project, we utilize Mongo (spring-boot-starter-data-mongodb 2.3.0.RELEASE) and require its use with @Transactional. For testing purposes, we set up an embedded Mongo with a replicaSet containing a single instance: EmbeddedMongoConfig @Configuration ...

reposition md-dialog labels on the fly

My tab dialog is not evenly arranged as shown below: https://i.sstatic.net/ghn4v.jpg Despite trying the flex directive, the tabs ("DESCRIPTION, MEANS,...) do not span the available space uniformly. Here is a shortened version of my HTML: <md-t ...

Updating environment variables in a React app without the need to rebuild the image

As I work on developing a Dockerized React application, I have encountered the challenge of defining environment variables for API URLs. React injects these variables during the build phase, meaning that I have to rebuild the entire image every time the en ...

Issue with Angular 12 service worker causing SW update to fail

I'm currently working on integrating a service worker into my Angular application to enable updates without user intervention. Here is the step-by-step process that I am following: Make changes to the application Run ng build Start an HTTP ser ...

When the responsive navbar is activated, it obscures the body content, causing a block in

When attempting to create a solution, ensure that the responsive toolbar is enabled on your Chrome browser. My approach involves designing for mobile first and then adapting for desktop. However, I am encountering an issue where the hamburger menu opens an ...