Alamofire upload image with multipart/form-data

天涯浪子 提交于 2019-12-11 02:24:17

问题


i am looking for a working sample code to upload an image with multipart/form-data using Alamofire

i can't get this code to work with my project

Alamofire.upload(
    .POST,
    URLString: "http://httpbin.org/post",
    multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
        multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { request, response, JSON, error in
                println(JSON)
            }
        case .Failure(let encodingError):
            println(encodingError)
        }
    }
)

This code has this error Could not find member 'POST'

some people said that if you use Alamofire.Method.POST it will fix the issue but i got this error instead Cannot invoke 'upload' with an argument list of type '(Method, URLString: String, multipartFormData: (_) -> _, encodingCompletion: (_) -> _)'

if anyone has explanation for this error or have another working sample , would appreciate your help.


回答1:


it was cocoapods installation issue i had to update the podfile to be like this

platform :ios, '8.0'
use_frameworks!

target 'ProjectName' do

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'master'

end
target 'ProjectNameTests' do
end

so that i can get the latest version of Alamofire that support MultipartFormData

for more information see this Answer



来源:https://stackoverflow.com/questions/31545620/alamofire-upload-image-with-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!