IOS Post upload image, file stream form

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.