zoukankan      html  css  js  c++  java
  • AFNetworking上传下载图片

     1 #pragma mark 上传图片
     2 -(void)Upload{
     3     
     4     UIImage * image = [UIImage imageNamed:@"iphone.png"];
     5     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     6     
     7     NSData *imageData = UIImageJPEGRepresentation(image, 1);
     8     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     9     formatter.dateFormat = @"yyyyMMddHHmmss";
    10     NSString *str = [formatter stringFromDate:[NSDate date]];
    11     NSString *fileName = [NSString stringWithFormat:@"%@", str];
    12     NSDictionary *parameters = @{@"filename":fileName};
    13     
    14     //申明请求的数据是json类型
    15     manager.requestSerializer=[AFJSONRequestSerializer serializer];
    16     //如果报接受类型不一致请替换一致text/html或别的
    17     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    18     
    19     [manager POST:@"http://192.168.40.10/IOSUPLOAD/WebForm1.aspx" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    20         
    21         // 上传图片,以文件流的格式
    22         [formData appendPartWithFileData:imageData name:@"img" fileName:fileName mimeType:@"image/png"];
    23         
    24     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    25         
    26         NSLog(@"%@",responseObject);
    27         
    28     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    29         
    30     }];
    31 }
    32 
    33 #pragma mark 下载图片
    34 -(void)Download{
    35     
    36     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    37     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    38     
    39     NSURL *URL = [NSURL URLWithString:@"http://192.168.40.10/FileDownload/WebForm1.aspx"];
    40     NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    41     
    42     NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    43         NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    44         return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    45     } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    46         NSLog(@"File downloaded to: %@", filePath);
    47     }];
    48     [downloadTask resume];
    49 }
    IOS开发
  • 相关阅读:
    16日彻底去除安卓应用的内置广告
    配台600元的主机套装 自己组装 全新
    带记录功能的计算器
    华为8812 进入工程模式 和打电话黑屏问题
    买平板 四核 500~600左右对比
    querySelector()方法
    Javascript实例教程:querySelector()方法接受一个CSS查询并返回匹配模式的第一个子孙元素,如果没有匹配的元素则返回null。
    Android实用代码七段(二)
    Android实用代码七段(三)
    Firebug入门指南
  • 原文地址:https://www.cnblogs.com/luanmage/p/5150732.html
Copyright © 2011-2022 走看看