zoukankan      html  css  js  c++  java
  • AFNetworking 文件上传Data,File图片,文件等上传

    一:AFNetworking的文件上传:

      主要几个以下类似 

    - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
                             name:(NSString *)name
                            error:(NSError * __autoreleasing *)error;

    二:主要代码:

        //配置文件上传
        
        //图片data 上传
        //UIImage *upImage = [UIImage imageNamed:@"testImage.png"];
        //NSData *imageData = UIImagePNGRepresentation(upImage);
        
        //文件file上传,上传mp3音乐文件 
        //NSString *theUpFilePath = [NSString stringWithFormat:@"%@testMusic.mp3",NSTemporaryDirectory()];
        
        //上传个图片文件;
        NSString *theImagePath = [[NSBundle mainBundle] pathForResource:@"testImage" ofType:@"png"];
        
        self.uploadFileClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kCOCOA_FileUPload]];
        NSMutableURLRequest *fileUpRequest = [_uploadFileClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            
            //[formData appendPartWithFileData:imageData name:@"file" fileName:@"testImage" mimeType:@"image/png"];
            
            //[formData appendPartWithFileURL:[NSURL fileURLWithPath:theUpFilePath isDirectory:NO] name:@"file" fileName:@"testMusic.mp3" mimeType:@"audio/mpeg3" error:nil];
            
            [formData appendPartWithFileURL:[NSURL fileURLWithPath:theImagePath] name:@"file" error:nil];
            
        }];
        
        self.fileUploadOp = [[AFHTTPRequestOperation alloc]initWithRequest:fileUpRequest];

    三:文件上传Demo

     1 #pragma mark 文件上传;
     2     
     3     //文件的 mine_type http://www.iana.org/assignments/media-types/media-types.xhtml
     4     
     5     UIProgressView *uploadFileProgressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
     6     uploadFileProgressView.center = CGPointMake(self.view.center.x, 100);
     7     uploadFileProgressView.progress = 0;
     8     uploadFileProgressView.progressTintColor = [UIColor blueColor];
     9     uploadFileProgressView.trackTintColor = [UIColor grayColor];
    10     [self.view addSubview:uploadFileProgressView];
    11     
    12     
    13     //开始
    14     UIButton *startUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    15     startUp.frame = CGRectMake(80, 120, 150, 30);
    16     [startUp setTitle:@"开始上传" forState:UIControlStateNormal];
    17     [startUp addTarget:self action:@selector(startUploadOP) forControlEvents:UIControlEventTouchUpInside];
    18     
    19     
    20     [self.view addSubview:startUp];
    21 
    22     
    23     //配置文件上传
    24     
    25     //图片data 上传
    26     //UIImage *upImage = [UIImage imageNamed:@"testImage.png"];
    27     //NSData *imageData = UIImagePNGRepresentation(upImage);
    28     
    29     //文件file上传,上传mp3音乐文件 
    30     //NSString *theUpFilePath = [NSString stringWithFormat:@"%@testMusic.mp3",NSTemporaryDirectory()];
    31     
    32     //上传个图片文件;
    33     NSString *theImagePath = [[NSBundle mainBundle] pathForResource:@"testImage" ofType:@"png"];
    34     
    35     self.uploadFileClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kCOCOA_FileUPload]];
    36     NSMutableURLRequest *fileUpRequest = [_uploadFileClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    37         
    38         //[formData appendPartWithFileData:imageData name:@"file" fileName:@"testImage" mimeType:@"image/png"];
    39         
    40         //[formData appendPartWithFileURL:[NSURL fileURLWithPath:theUpFilePath isDirectory:NO] name:@"file" fileName:@"testMusic.mp3" mimeType:@"audio/mpeg3" error:nil];
    41         
    42         [formData appendPartWithFileURL:[NSURL fileURLWithPath:theImagePath] name:@"file" error:nil];
    43         
    44     }];
    45     
    46     self.fileUploadOp = [[AFHTTPRequestOperation alloc]initWithRequest:fileUpRequest];
    47     
    48     [_fileUploadOp setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    49         startUp.enabled = NO;
    50         [startUp setTitle:@"正在上传" forState:UIControlStateNormal];
    51         CGFloat progress = ((float)totalBytesWritten) / totalBytesExpectedToWrite;
    52         [uploadFileProgressView setProgress:progress animated:YES];
    53         
    54     }];
    55     
    56     [_fileUploadOp setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    57         startUp.enabled = NO;
    58         [startUp setTitle:@"完成" forState:UIControlStateNormal];
    59         NSLog(@"upload finish ---%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
    60         
    61     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    62         NSLog(@"error %@",error);
    63     }];
    64     
    65     
    66     
    67     
    68     
    69     
    70     
    71 }
    72 
    73 #pragma mark AFNetworking 文件上传
    74 
    75 - (void)startUploadOP
    76 {
    77     [_fileUploadOp start];
    78 }

    四:关于文件上传服务器,建议使用php,在 windows 上下载个 wamp 一键就配置成了php 服务器

         php 文件上传接口:

        查看我的文章:http://www.cnblogs.com/cocoajin/p/3491371.html

  • 相关阅读:
    leetcode Remove Linked List Elements
    leetcode Word Pattern
    leetcode Isomorphic Strings
    leetcode Valid Parentheses
    leetcode Remove Nth Node From End of List
    leetcode Contains Duplicate II
    leetcode Rectangle Area
    leetcode Length of Last Word
    leetcode Valid Sudoku
    leetcode Reverse Bits
  • 原文地址:https://www.cnblogs.com/cocoajin/p/3491452.html
Copyright © 2011-2022 走看看