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
}
console.log(req.body)
console.log(req.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.