2

I post here because I'm developping an API with SailsJS , and when I want to upload a file with an android app client , I've got a problem.

Req.files / req.file('myfile') is empty.

This my source to perform upload :

//FileController.js

upload: function (req, res) {
  console.log("REQ ", req.file('image'));
  console.log(req.files.image.originalFilename);
  console.log(req.files.image.path);
  req.file('image').upload({ dirname: '../../assets/images/profile/'},function (err, uploadedFiles){

    if (err){
      return res.serverError({
        code   : 500,
        status : res.i18n('error'),
        notice : res.i18n('An error has occurred.'),
        object : { error:error }
      });
    } else{
      return res.ok({
              code   : 200,
              status : res.i18n('OK'),
              notice : res.i18n('Avatar uploaded.'),
              object : uploadedFiles
          })
    }
    
  });
 }

I work with Sails v0.11.0

Nota. : My android client app works great with an other nodeJS app.

Do you have an idea to fix problem ?

Thanks.

outstore
  • 175
  • 2
  • 13
  • Just a guess, but it could be the fact that "Text parameters must be included before files in the request body." (http://sailsjs.org/#!/documentation/reference/req/req.file.html). Your Android client might be mangling the order; it might be worth checking out. – Alex Alksne May 02 '15 at 13:18
  • Check my answer to this question, contains complete image upload code with android and sailsjs http://stackoverflow.com/a/43839761/3136282 – Arshad May 08 '17 at 04:32

2 Answers2

1

How do you send your file to the server? you should send the param 'image' with the value equal to the file({image: yourfile})

Matan Gubkin
  • 3,019
  • 6
  • 25
  • 44
  • Hi, I send the file, with my android application , like this : Future uploading = Ion.with(MainActivity.this) .load("http://www.example.com/user/upload") .setMultipartFile("image", f) .asString() .withResponse() .setCallback(new FutureCallback>() { } }); – outstore May 03 '15 at 15:01
  • try using postman to see if the problem is in the server or the client – Matan Gubkin May 03 '15 at 17:49
  • Why I can see it with postman ? ... Can I upload with postman ??? .... Else this is the print trace in the server side terminal : REQ { _fatalErrors: [], _files: [], timeouts: { untilFirstFileTimer: { _idleTimeout: 10000, _idlePrev: [Object], _idleNext: [Object], .... _files array is empty --> I don't understand . – outstore May 03 '15 at 18:04
  • files array is empty => it cannot find the file. Have you tried posting the request using postman? make you're sending as 'post' to the currect url and a file – Matan Gubkin May 03 '15 at 18:13
  • When I test just with text parameters , Post request works successfully. But when I want to test with a file sent by the android client, this fails. – outstore May 03 '15 at 19:43
  • Go to this link https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?utm_source=chrome-ntp-icon and add the postman app, open it up. In the url type: http://yourserver:1337/upload and change the type of the request to 'POST'. Now, in the key value type 'image' and change the type to file instead of text and add a file. If it still returns an error then your server side code is wrong. Go ahead to here: http://sailsjs.org/#!/documentation/concepts/File-Uploads and go through again on your code. – Matan Gubkin May 05 '15 at 04:29
  • This is my result with postman : `{ "code": 200, "status": "SUCCES", "notice": "Avatar uploaded.", "object": [ { "fd": "/Users/louis/Desktop/GITHUB/RadioMed/RadioMedAPI/assets/images/profile/e0f826e1-6259-4da3-bcb1-385c48fee966.jpg", "size": 372831, "type": "image/jpeg", "filename": "10535750_10205839147482603_4263140377093928704_o.jpg", "status": "bufferingOrWriting", "field": "image" } ] }` – outstore May 06 '15 at 13:20
  • That means the file has been uploaded. You problem is with the client who doesn't send the file to the sails server. Please go through again on your client code. Your problem has nothing to do with sails.js – Matan Gubkin May 06 '15 at 13:23
  • I solved my issue. My problem was that I've uncommentted old code in my android upload client. Thanks everybody ! – outstore May 08 '15 at 13:18
-1

When upload file, html form must be set enctype as multipart/form-data.

Example:

<!-- enctype="multipart/form-data" -->
<form id="uploadForm"
    enctype="multipart/form-data"
    action="/file/upload"
    method="post">
        <input type="file" name="uploadFile" />
        <input type="submit" value="submit"/>
</form>
Anthon
  • 69,918
  • 32
  • 186
  • 246
Du Luong
  • 124
  • 1
  • 4