I'm trying to upload image data from iOS using Alamofire to an Express server with Multer. req.file
is undefined, and req.body
is in the form { file: <bytes> }
. There is no error message, but the file does not appear. 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')
})
On 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)
}
})
This has puzzled me for hours, any help is greatly appreciated!
UPDATE An HTML form worked fine through the Express endpoint, so it's definitely a problem with the request Alamofire is sending. I've tried a bunch of examples of uploading with Alamofire, but they all send the same incorrect request. There must be a way to make the same request as an HTML form but with Alamofire.
ANOTHER UPDATE
I'm now just using busboy-connect and it's working well, and with a lot more flexibility.
Here is a simple function that requires the target upload url, parameters, and imageData and returns the URLRequestConvertible and NSData that Alamofire. upload requires to upload an image with parameters. almost right, uploadData. appendData("Content-Disposition: form-data; name=\"file\"; filename=\"file.
The following code will go in the app.const multer = require('multer'); const upload = multer({dest:'uploads/'}). single("demo_image"); Here, we have called the multer() method. It accepts an options object, with dest property, which tells Multer where to upload the files.
Upload Video Files using Multer const videoStorage = multer. diskStorage({ destination: 'videos', // Destination to store video filename: (req, file, cb) => { cb(null, file. fieldname + '_' + Date. now() + path.
I was just able to get this working. It turns out that you have to specify the fileName and the mimeType using Alamofire in order for multer to pick up the upload on the server end. So, your code for adding the image should look something like this:
if let image = image {
if let imageData = UIImageJPEGRepresentation(image, 0.5) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "fileName.jpg", mimeType: "image/jpeg")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With