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 }
时间: 2024-10-06 00:48:16