0

Hi I am currently looking for library that uses AWS Lambda Proxy Integration service from amazon that would help me parse formdata and send the data using https library using nodejs. A sample code will do as well on how to get the body and the file from the formdata request.

Please someone let me know if there is one. Your help would be much appreciated.

2
  • Are you looking for the front-end code OR server side form handling code Commented Aug 30, 2021 at 6:40
  • Hi @SubhashisPandey, I am looking for server side form handling code. Commented Aug 30, 2021 at 7:17

1 Answer 1

1

Try this code, complete the form element as per your need.

HTML form

    <form id="hfrm" method="POST" action="/fileUpload" enctype="multipart/form-data">

    </form>

Use AJAX to submit the code

var form = $('#hfrm').get(0); 
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "URL");
xhr.send(formData);

The server side code to handle the request, please note there are few places where you must use credentials and names like AWS Region, S3 bucket name etc.

    var xpress          = require('express')
    const fileUpload    = require('express-fileupload');
    const app           = xpress();

    app.use(fileUpload());


    var AWS = require('aws-sdk');

    app.post('/fileUpload', async (req, res) => {

        AWS.config.update({
            accessKeyId: "ACCESS-KEY",
            secretAccesskey: "SECRET-ACCESS-KEY",
            region: "AWS REGION"
        })


        const s3 = new AWS.S3();

        const fileContent  = Buffer.from(req.files.uploadedFileName.data, 'binary');

        const params = {
                Bucket: 'S3 BUKET-NAME',
                Key: "Name of the File",
                Body: fileContent 
        };

        s3.upload(params, function(err, data) {
                if (err) {
                    throw err;
                }
                res.send({
                    "response_code": 200,
                    "response_message": "Success message",
                    "response_data": data
                });
        });

    })

    app.listen(3000, function () {
        console.log('Sample app listening on port 3000!');
    });
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I am not using AWS S3 bucket service because we handle the data in our own database, so basically we will upload the file in our own database.
You have the file content, so can omit the S3 section and write your own DB code instead
ohhh ok ok i see now, but how about the formdata body content? and how to pass the formdata body including the file to the vpce api server using https library?
You can use JQuery Form Submit, if using API, editing my answer to add the Form submit code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.