Recently saw a lot of small friends in the group have asked to upload the image server can not receive, parse. It can be said that an attribute ContentType, whether you use a third-party AFN, or your own encapsulation nsurlsesstion. All need to put ContentType settings and the same way in the background (perhaps the term is not very standard, in short, this means Ah, understanding)
The following post a AFN upload image code, pro-Test effective, but may be only for my server. Just communicate with the backstage classmates.
This body is passed to the background parameters, because it is uploading pictures, so just give a picture name is enough, this and backstage to ask
Nsdictionary * BODY = @{@ "category": @ "User" @ "file": @ "headimg"};
Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];
ContentType Settings
Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@ "Application/json",
@ "Text/html",
@ "Image/jpeg",
@ "Image/png",
@ "Application/octet-stream",
@ "Text/json",
NIL];
manager.requestserializer= [Afhttprequestserializer Serializer];
manager.responseserializer= [Afhttpresponseserializer Serializer];
[Manager Post:url Parameters:body constructingbodywithblock:^ (id<afmultipartformdata> _Nonnull formData) {
Convert image to data, post upload can only transmit data
NSData *data = uiimagepngrepresentation (image);
Uploaded parameters (upload image to file stream format)
[FormData Appendpartwithfiledata:data
name:@ "File"
filename:@ "Gauge.png"
mimetype:@ "Image/png"];
} progress:^ (Nsprogress * _nonnull uploadprogress) {
} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {
Request a successful block callback
Nsdictionary * dic = [nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingallowfragments Error : nil];
NSLog (@ "Upload successful%@", DIC);
} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {
NSLog (@ "Upload failed%@", error);
}];
* If you feel not enough fun iOS sister can add QQ 751045314, Ah Wei do not mind our deep communication Oh, to demo left QQ e-mail
IOS Post upload image, file stream form