What is the best way to pass the value of a selected option to an express server

<label for="exampleFormControlSelect1">
  <strong>Please Select the Number of PDFs to Merge:</strong>
</label>
<select class="form-control" id="exampleFormControlSelect1">
  <option name="number">1</option>
  <option name="number">2</option>
  <option name="number">3</option>
  <option name="number">4</option>
  <option name="number">5</option>
</select>
app.post("/merge", upload.array("pdfs", 2), async (req, res, next) => {
  console.log(req.files);
  let d = await mergePdfs(
    path.join(__dirname, req.files[0].path),
    path.join(__dirname, req.files[1].path)
  );
});

I have hard-coded the option for merging 2 PDFs but the user can select any number.

Answer №1

  1. Modify the Multer middleware to allow for unlimited file uploads.

    upload.array("pdfs")
    
  2. Ensure that your <select> element has a designated name attribute to be included in the request payload.

    <select
      required
      name="limit"
      class="form-control"
      id="exampleFormControlSelect1"
    >
    
  3. Verify the length of req.files against req.body.limit and handle any discrepancies by extracting the file paths and passing them to mergePdfs()

    app.post("/merge", upload.array("pdfs"), async (req, res, next) => {
      const { limit } = req.body;
      if (req.files.length !== limit) {
        return res.status(400).send(
          `Expected ${limit} file(s), received ${req.files.length}`
        );
      }
    
      try {
        const result = await mergePdfs(
          ...req.files.map(({ path: filePath }) => filePath)
        );
    
        // Handle the merged PDF document here
      } catch (err) {
        next(err);
      }
    

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

Node.js module loader compared to client-side AMD loader such as RequireJS

Today, let's talk about loading Javascript modules on the client-side. There are two popular ways to do this: Using RequireJS Utilizing NPM (Node Package Manager) for exporting and requiring files I've always found the first option to work wel ...

Sign up for a Jquery template event

When utilizing a jquery template, the following HTML markup is being used: <div id="results"> <div class="CommentItem" commentid="33064" id="33064" data-guid="/Profile/Profile.aspx?id=Charliedog33"> <div class="CommentPic" ...

What steps can I take to troubleshoot and fix the height of a div/span element?

Here is my HTML code: <div> <span style="display: inline-block;height:20px">20</span> <span style="display: inline-block;"><img src="img/ex.png"/></span> </div> The image I have in the second span is sized at ...

Aurelia validator fails to refresh user interface

Despite the aurelia-validator plugin working correctly for form submission and validation, with all properties updating properly, the UI does not reflect any changes. There is no red outline around incorrect properties or error messages displayed. I have r ...

Utilizing Javascript / jQuery to eliminate specific CSS styles

I am facing an issue with the CSS code for a table positioned at the bottom of the screen. The current code includes a filter specifically for IE 8, but I need to make it compatible with IE 10 as well by removing the filter and adding a background color. ...

Is there a way to retrieve all "a" tags with an "href" attribute that contains the term "youtube"?

My goal is to capture all a tags that have the href attribute containing the word youtube. This task requires the use of jquery. ...

Rails confirmation feature malfunctioning

I have been struggling to figure out where I am going wrong with this code even though I've checked several posts. I am using Ruby on Rails and trying to run the following snippet: <%= link_to 'Destroy', article_path(article), ...

When utilizing the ExpressJS API to retrieve GET requests in C++, the response string can only be used for printing purposes and nothing else

Issue: I am currently working on a C++ program that interacts with an ExpressJS API to retrieve files and strings. While I have successfully managed to download text files using curlRequests, I am encountering difficulties when attempting to handle plain s ...

The Protected Routes in React Router Dom persistently redirecting

I am currently implementing protected routes in my Redux and Pure React app using React Router Dom. The issue I am facing is that when I navigate to /profile/edit and then refresh the page, it automatically redirects me to /login and then to /profile. I ha ...

Angular.js dynamically changing ng-class based on long-polling updates to a monitored variable

Currently utilizing angular.js for my project. I am implementing long polling with a server and would like to dynamically update an element in the view, specifically one with a class of 'updated', whenever the value of build_tag is incremented. ...

Can dynamic attributes be used with ternary operators in Angular?

I attempted to alter the id of a div using Angular and implemented the following code: <div [id]="'item_' + (itemName !== undefined ? itemName.replace(' ', '-').toLowerCase() : '')"> However, when I run my te ...

What is the best way to capture dynamic import errors in JavaScript?

I am currently developing a website using Next.js. My goal is to utilize dynamic import import() to load a module dynamically, even if it may not exist. If the module does not exist, I am fine with suppressing it: const Blog = async () => { let L ...

Display a specific element only if another element exceeds a specified height

A snippet of HTML code is given below: <span class="day-number">{{day-number}}</span> <div class="event-box"> <div class="event-container"> </div> <div class="more-events">more ...</div> </div> The .e ...

Frequently refreshing a page to display the most recent information without needing to reload the entire

I am seeking a solution for updating the comments section on my website live or every 30 seconds. The comments are fetched from a MySQL database using PHP: <?php $link = mysql_connect('localhost', 'root', ''); ...

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 ...

Customize the appearance of alert boxes in ajax requests

Is there a way to customize the default ajax alert box? I currently have code that deletes a user and reloads the current page. It functions properly, but I want to enhance the styling of the alert box that appears before the user is deleted. function d ...

Create an excel spreadsheet using HTML tables with the help of jQuery

Upon clicking a button, I aim to generate an excel sheet. Essentially, I am trying to achieve what is being discussed here (Kalle H. Väravas answer). If the following links are not working within Mozilla browser, then maybe my code requires ActiveXObject ...

The HTML file contains the complete version number of Firefox

If you're an expert in HTML, take a look at my expandable start page. I know there's room for improvement, and I'm seeking advice on automatically updating the Firefox browser version number in line 106 of the code snippet below. (Linux Mint ...

Is it possible to create an animation in NextJS using framer-motion that triggers only during the initial page load and resets every 12 hours?

My website has a main page that loads in with an exciting framer-motion animation. I'm trying to figure out how to make sure this animation only plays the first time the page is loaded, and not every time the page is refreshed or navigated back to. Si ...

Accessing the app module in separate files is not possible for Angular and Coffeescript

As I work on managing and refactoring my Angular code in a Rails project with CoffeeScript, I am facing issues accessing Angular objects between multiple files. Here is the current file structure: javascripts |-Angular |-controllers | |-search_strl.js ...