iOS Utilizes AFNetworking to Realize File Upload Sample Code

  • 2021-11-02 02:57:58
  • OfStack

0. Import Framework Preparation

1. Drag and drop the framework program into the project

2. Add an iOS framework reference


 In fact, in fact, the SystemConfiguration.framework

 In fact, in fact, the MobileCoreServices.framework

3. Introduce


#import "AFNetworking.h"

4. Modify the xxx-Prefix. pch file


#import <MobileCoreServices/MobileCoreServices.h>

#import <SystemConfiguration/SystemConfiguration.h>

1. The client of 1. AFN is initialized with basic address and instantiates an operation queue at the same time, so as to facilitate subsequent multithreading


@interfaceViewController ()

{

  // AFN Initialized with the base address and instantiates the client of 1 Operation queues for subsequent multithreading 

  AFHTTPClient  *_httpClient;
  NSOperationQueue *_queue;

}
- (void)viewDidLoad
{
  [super viewDidLoad];
  
  NSURL *url = [NSURL URLWithString:@"http://192.168.3.255/~apple/qingche"];
  _httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
  
  _queue = [[NSOperationQueue alloc] init];
}

2. Using AFN to realize the details of file uploading operation


#pragma mark -  File upload 
- (IBAction)uploadImage
{
  /*
    If this code needs to be modified, the position can be adjusted 
   
   1.  Put upload.php Change to the address told by the website developer 
   2.  Put file Change to the field name told by the website developer 
   */
  // 1. httpClient->url
  
  // 2.  Upload request POST
  NSURLRequest *request = [_httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //  Generate at this location 1 Data bodies to upload 
    // form Corresponding to this is html Forms in Files 
    
    
    UIImage *image = [UIImage imageNamed:@" Head portrait 1"];
    NSData *data = UIImagePNGRepresentation(image);
    
    //  In network development, when uploading files, files are not allowed to be overwritten, and files have duplicate names 
    //  To solve this problem, 
    //  You can use the current system event as the file name when uploading 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //  Format Time 
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
    
    
    /*
      This method parameter 
     1.  To upload [2 Binary data ]
     2.  On the corresponding website [upload.php Medium ] Object that handles files [ Field "file"]
     3.  To save on the server [ Filename ]
     4.  Upload the file [mimeType]
     */
    [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"];
  }];
  
  // 3. operation Packaged urlconnetion
  AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  
  [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@" Upload complete ");
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@" Upload failed ->%@", error);
  }];
  
  // Execute 
  [_httpClient.operationQueue addOperation:op];

Related articles: