Sails.js : post text input and a file in the same time
Asked Answered
U

2

5

I want to send a file and a hidden input text in a form.

<form method="POST" action="/api/import_xlsx_data" enctype="multipart/form-data">
<input type="file" name="xlsx_file_to_import" accept=".xlsx" required>
<input id="url" type="HIDDEN" name="url" value="url-value">
<input type="submit" value="Envoyer">

In my controller request.body is equal to {}.
When I remove enctype="multipart/form-data" it works for my text but not for my file.

To upload my file :

uploadFile.upload({saveAs : fileName, dirname : directoryName},function onUploadComplete(err, files) { ...............});

My controller :

 importXLS: function (req, res) {
    var uploadFile = req.file('xlsx_file_to_import');
    //console.log(req.params()); -> send error params is not a function
    console.log(req.body); // send me {}
    console.log(req.param('url')); //send me undefined


...... }

More code on pastbin : My view : view
My controller : controller

Unconditioned answered 9/4, 2015 at 15:43 Comment(0)
P
5

Using skipper as a body parser, you must send the text parameters before your file input.

Try this:

<form method="POST" action="/api/import_xlsx_data" enctype="multipart/form-data">
  <input id="url" type="HIDDEN" name="url" value="url-value">  
  <input type="file" name="xlsx_file_to_import" accept=".xlsx" required>
<input type="submit" value="Envoyer">

For more information, please see the documentation for skipper : https://github.com/balderdashy/skipper#text-parameters

Prelude answered 4/6, 2015 at 13:59 Comment(0)
T
2

To retrieve your field, you need to use :

request.param("url")

And your file with

var file = request.file("xlsx_file_to_import");
Thruster answered 9/4, 2015 at 16:3 Comment(11)
My file is uploaded but I can not get url with req.param("url"). It's undefined.Unconditioned
Can you post you Controller code please ? and tell me what request.params() returnThruster
Sorry it's request.params it's not a function, uploadFile.upload is used on your front or your back ? Cause if it's from maybe is only send the file and not the full form. You can also try to add another field on your form to see if it's change somethingThruster
uploadFile is used on my back. If I add an other input ( not hidden ) it does not change anything.Unconditioned
Strange... I don't have other ideas sorry :/ your form have a </form> ? cause you don't write it. Don't put it hidden to see if it's change something...Thruster
I think my form is ok. I add pasbin links. Hidden or not I can not see any change.Unconditioned
can it be a async problem ?Unconditioned
Don't think, I just look your controller code and try to delete var request = require('request'); pleaseThruster
It doesn't change anything after delete request = require('request');Unconditioned
Do you have any policies who can touch the req object ?Thruster
Let us continue this discussion in chat.Unconditioned

© 2022 - 2024 — McMap. All rights reserved.