Download files using Alamofire and Multer

I am trying to upload image data from iOS using Alamofire to an Express server with Multer. req.file- undefined, but req.bodyhas the form { file: <bytes> }. There is no error message, but the file is not displayed. Here is my code:

var bodyParser = require('body-parser')
var multer = require('multer')

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.post('/api/photos/upload', function(req, res) {
    var upload = multer({ dest: 'public/images/content/'}).single('file')
    upload(req, res, function(err) {
        if (err) {
            console.log("Error uploading file: " + err)
            return
        }

        // req.file = req.body
        console.log(req.body) // form fields
        console.log(req.file) // form file
    })

    res.json('yeah')
})

In iOS:

let url = fullURL("api/photos/upload")

Alamofire.upload(.POST, url, multipartFormData: { multipartFormData in

        if let image = image {
            if let imageData = UIImageJPEGRepresentation(image, 0.5) {
                multipartFormData.appendBodyPart(data: imageData, name: "file")
            }
        }

        }, encodingCompletion: { encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in

                    switch response.result {
                    case .Success:
                        print("success")
                    case .Failure(let error):
                        print(error)
                    }

                }
            case .Failure(let encodingError):
                print(encodingError)
            }

    })

It puzzled me for several hours, any help is much appreciated!

The UPDATE HTML form worked fine through the Express endpoint, so there is definitely a problem sending the Alamofire request. I tried a bunch of download examples using Alamofire, but they all send the same invalid request. There should be a way to make the same request as the HTML form, but with Alamofire.

OTHER UPDATE
Now I just use busboy-connect and it works well and with much more flexibility.

+4
3

. , fileName mimeType, Alamofire, . , :

if let image = image {
    if let imageData = UIImageJPEGRepresentation(image, 0.5) {
        multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "fileName.jpg", mimeType: "image/jpeg")
    }
}
+6

, , multer :

var upload = multer({ dest: 'public/images/content/'})

app.post('/api/photos/upload', upload.single('file'), function(req, res) {
  // req.file should be populated now
})

, :

app.post('/path',
  middleware1,
  middleware2,
  middleware3,
  ...,
  function(req, res) {
    // All middlewares has been executed
  })
0

, multipartFormData.append(value.data, withName: name, fileName: filename, mimeType: mimeType) multipartFormData.append(value.data, withName: key).

, multer req.file, req.body.

0

Source: https://habr.com/ru/post/1629741/


All Articles