Alan Alan - 5 months ago 24
Swift Question

Alamofire upload "name" parameter

In the Alamofire

upload
code there is a
name
and
fileName
field.

Alamofire.upload(
.POST,
"http://192.168.1.241:8080/file/user",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file", mimeType: "image/jpeg")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
dispatch_async(dispatch_get_main_queue()) {
let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
//progress(percent: percent)
print(percent)
}
}
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
debugPrint(encodingError)
}
}
)


In my Spring server I looking for
@RequestParam("file") MultipartFile file
and I could not get it to work because I was setting
name
to
file
. But apparently I needed to set the
fileName
to
"file"
.

What exactly is the purpose of
name
if it doesn't seem to be used for request parameter mapping?

Answer

For "file" it means the full NSURL property of your file (example: www.mysite.com/myvideo.mp4), for "filename" just only the lastPathComponent in String format (example: video.mp4)

Comments