我正在使用Rubymotion創建iOS應用程序。我正在嘗試製作圖像的分段上傳 。無法使用AFNetworking上傳圖片
我用這個代碼,以使上傳,但我得到的錯誤:
data = {token: "2xCGdzcuEeNzhst3Yaa8f", task: 1, message: "message", latitude: 1, longitude: 1}
client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/"))
request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data|
image_data = UIImagePNGRepresentation(image.image)
form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png')
})
operation = AFJSONRequestOperation.alloc.initWithRequest(request)
operation.setCompletionBlockWithSuccess(lambda{ |operation, responseObject| puts 'all done!'})
我得到這個錯誤:
undefined method `setCompletionBlockWithSuccess' for #<AFJSONRequestOperation:0xb227f00> (NoMethodError)
從我所看到的,操作是有效的對象並應該有這種方法。 我在這裏錯過了什麼嗎?
更新1
這是我更新的代碼,但是當我運行它似乎POST操作不火。我仍然缺少任何代碼?我也沒有得到任何錯誤。
data = {token: "2xCGdzcuEeNzhs5tYaa8f", task: 1, message: "message", latitude: 1, longitude: 1}
client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/"))
request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data|
image_data = UIImagePNGRepresentation(image.image)
form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png')
})
operation = AFJSONRequestOperation.alloc.initWithRequest(request)
operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'},
failure: lambda { |operation, error| puts 'error' })
請求對象輸出這樣的:
<NSMutableURLRequest:195641792 - url: http://api.example.com/api/v1/messages,
headers: {"Content-Length"=>"118200", "Content-Type"=>"multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY", "Accept-Language"=>"en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8", "User-Agent"=>"theapp/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)"},
cache policy: 0, Pipelining: false, main doc url: , timeout: 60.0, network service type: 0 >
操作對象輸出這樣的:
<AFJSONRequestOperation:0xa78c660>
謝謝!我現在擺脫了錯誤信息,但POST操作似乎沒有啓動。請看我更新的問題。 –
你真的開始了這個請求嗎? 'operation.start' –