ios-using Afnetworking (AFN)-Upload images

Source: Internet
Author: User

ios-using afnetworking (AFN)-Upload images 

Official advice on how to use AFN

1. Define a global afhttpclient: Contains

1> BaseURL

2> Request

3> Operation Queue Nsoperationqueue

2. Responsible for all network operation requests by Afhttprequestoperation(HTTP request operation)

3. Modify the Xxx-prefix.pch file

#import <MobileCoreServices/MobileCoreServices.h>

#import <SystemConfiguration/SystemConfiguration.h>

0. Import Framework Preparation Work

1. Dragging a frame program into a project

·2. Add iOS Framework Reference

–systemconfiguration.framework

–mobilecoreservices.framework

• 3. Introduced

#import "AFNetworking.h"

1.AFN client, initialized with base address, and instantiates an operations queue for subsequent multithreading2. Using AFN to achieve file upload operation fine1 #pragma mark-File upload
/* 5 If you need to modify this code, you can adjust the position 6 7 1. Change the upload.php to the site developer's address 8 2. Change the file to the field name 9 *///1 for the website developer . httpClient URL//  AFN client, initializing with base address, and instantiating an operations queue for subsequent multithreading    
5 Nsurl *url = [Nsurl urlwithstring:@ "Http://192.168.3.255/~apple/qingche"];  6    afhttpclient *_httpclient = [[Afhttpclient alloc] initwithbaseurl:url];
  Nsoperationqueue*_queue = [[Nsoperationqueue alloc] init];
12//2. Upload Request Post13nsurlrequest*request = [_httpclient multipartformrequestwithmethod:@"POST "Path:@"upload.php "Parameters:nil constructingbodywithblock:^ (id<afmultipartformdata> formData) {14//Generate a data body to upload at this location,The form corresponds to the forms in the HTML file UIImage *image = [UIImage imagenamed:@"Avatar 1 "];NSData *data = uiimagepngrepresentation (image); 21st//In the network development, when uploading a file, the file is not allowed to be overwritten, the file name,To resolve this issue,You can use the current system event as the file name at upload timeNSDateFormatter *formatter = [[NSDateFormatter alloc] init];25//Set the time formatFormatter.dateformat =@"Yyyymmddhhmmss ";NSString *str = [formatter stringfromdate:[nsdate date]];NSString *filename = [NSString stringWithFormat:@"%@.png ", str]; 31/*32 This method parameter 33 1. [Binary data] 34 2 to upload. [Field ' file '] 35 3 for processing files [in upload.php] on the website. [FileName] 36 4 to be saved on the server. Upload the file [MimeType] 37 */
[FormDataAppendpartwithfiledata:data Name:@"File "Filename:filename MimeType:@"Image/png "];39}];41// 3. Operation (HTTP request operation) wrapped Urlconnetion42 afhttprequestoperation *httprope = [[Afhttprequestoperation alloc] initwithrequest:request]; 44 [Httprope setcompletionblockwithsuccess:^ (Afhttprequestoperation * operation, id responseobject) {45 NSLog (@ " upload complete"); Span class= "number" >46} failure:^ (Afhttprequestoperation *operation, Nserror *error) {47 NSLog ( Span class= "string" >@ " upload failed->%@", error); 48}]; 50 // execute 51 [_httpclient.operationqueue addoperation : op];

ios-using Afnetworking (AFN)-Upload images

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.