Hello,
do you have any timeframe for Multipart POST?
I would love to use Alamofire, but it's a show stopper for me.
Until this is implemented, AFNetworking's multipart form request functionality can be used in conjunction with Alamofire itself.
I'm going to use AFNetworking for now as suggested, but I'd love to see this in Alamofire as well!
AFNetworking is a lot of overhead just because of one of its many features ... ^^
Same here. Do you have an idea of when it will be implemented?
same here.I implemented most of my networking with Alamofire and just integrated AFNetworking for the multipart request...I would really love to see this feature implemented and great work so far, thanks!
+1
Would love to have it in Alamofire too
Here is a quick&dirty patch I use: http://pastebin.com/k9CUJjry
Using AFNetworking works fine though.
+1 who wants multipart support
@db0company why not make a PR for this?
@mente Because this is a quick&dirty solution that works for me but might not work for everybody since it's not complete and has hardcoded strings.
+1 woo and thanks to @db0company - that patch is dope. nyan.
@mattt looking to implement this using AFNetworking, any estimate on when it will be implemented in Alamofire?
I've created a class function to upload single file as multipart/form-data based on this SO answers.
class Photo {
class func upload(image: UIImage, filename: String) -> Request {
let route = Router.CreatePhoto()
var request = route.URLRequest.mutableCopy() as NSMutableURLRequest
let boundary = "NET-POST-boundary-\(arc4random())-\(arc4random())"
request.setValue("multipart/form-data;boundary="+boundary,
forHTTPHeaderField: "Content-Type")
let parameters = NSMutableData()
for s in ["\r\n--\(boundary)\r\n",
"Content-Disposition: form-data; name=\"photos[photo]\";" +
" filename=\"\(filename)\"\r\n",
"Content-Type: image/png\r\n\r\n"] {
parameters.appendData(s.dataUsingEncoding(NSUTF8StringEncoding)!)
}
parameters.appendData(UIImageJPEGRepresentation(image, 1))
parameters.appendData("\r\n--\(boundary)--\r\n"
.dataUsingEncoding(NSUTF8StringEncoding)!)
return Alamofire.upload(request, parameters)
}
}
let rep = (asset as ALAsset).defaultRepresentation()
let ref = rep.fullResolutionImage().takeUnretainedValue()
Photo.upload(UIImage(CGImage: ref)!, filename: rep.filename())
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println(totalBytesWritten)
}
.responseJSON { (request, response, JSON, error) in
println(JSON)
}
@tomoyuki28jp I don't have my head around Alamofire routes yet, can you provide the rest of the code for the Photo example?
@captainchung Take a look at those SO answers.
http://stackoverflow.com/a/26747857/3958295
http://stackoverflow.com/a/27014372/3958295
@mattt
Until this is implemented, AFNetworking's multipart form request functionality can be used in conjunction with Alamofire itself.
Shortly after you responded (8807fb86bf2e6f8ba979c3ab23bc3903eeab3c15), you added this to the README:
Use AFNetworking for any of the following:
[...]
Multipart HTTP request construction
Sorry to bother you, but could you possibly clarify as to whether you still intend to implement this?
+1
@tomoyuki28jp
how can i set the params with this input.
PATCH
"/api/accounts/1"
Parameters: {"user"=>
{"first_name"=>"Gretchen",
"last_name"=>"Gapol",
"profile_photo"=>#oadedFile:0x007fab541ee658 @tempfile=#
}
i want to put the image param in "profile_photo"
@mileswd we are going to implement this. We haven't finalized the 1.3.0
roadmap yet, but most likely this feature will make it onto that list. Stay tuned...
@cnoon Glad to hear this! Thanks for the update :+1:
Any update ?
In the meanwhile I have created a very general solution, based on previous comments and stackoverflow questions.
It does not require Routes just a NSURLRequest.
It allows to add multiple files to the multipart request, as well as other parameters or HTTP headers.
No hardcoded options, no assumptions: you can upload any kind of file, not just photos.
http://stackoverflow.com/a/26747857/3958295
It's useful!!!
Thk God. I fixed problem.
Hey everyone, I just put up PR #539 which adds support for MultipartFormData
uploads. I would love your feedback before merging it in. Cheers.
You can try this, it is working
Alamofire.upload(
.POST,
URLString: "http://blankapp.io/api/v1/update",
multipartFormData: {
multipartFormData in
multipartFormData.appendBodyPart(fileURL: NSURL(fileURLWithPath: imagePath)!, name: "cover")
multipartFormData.appendBodyPart(data: "Alamofire test title".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "title")
multipartFormData.appendBodyPart(data: "test content".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "content")
multipartFormData.appendBodyPart(data: "1".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "type")
},
encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _ ):
upload.responseJSON {
request, response, JSON, error in
println(JSON)
}
case .Failure(let encodingError):
println("Failure")
println(encodingError)
}
}
)
Can you tell how much time (roughly) takes to implement it?
@cnoon: Do you have any tricks up your sleeve for diagnosing stalled uploads? I am logging in progress
, the backend in this case is Django Rest Framework on Heroku. What I am seeing is that the upload pauses - my logs stop, and network activity goes to zero. I am running on Wifi and otherwise have good bandwidth.
(Thanks)
Hi @chrisco314, I've seen some weird throttling on uploads on some ISPs before, but generally, it should just upload consistently. Alamofire doesn't do anything to trigger that behavior in any way.
@cnoon :
How can I use public fund upload(
URLRequest: URLRequestConvertible,
multipartFormData: MultipartFormData -> Void,
encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
method to upload a file with some parameters?
I tried by setting parameters as data for the URLRequest
and adding file to multipartFormData
, but the server is not getting the parameters and throwing some error.
Is this correct or should I add parameters as data to multipartFormData
?
I am using Alamofire.ParameterEncoding.URL
for parameter encoding, should I change this to custom and append data with that?
You need to add the parameters to the multipart form data @Johnykutty. 馃憤馃徏
Most helpful comment
+1 who wants multipart support