AFNetworking文件上传Data,File图片,文件等上传(转) afnetworking断点上传

一: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 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 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)startUploadOP76 {77     [_fileUploadOp start];78 }

AFNetworking文件上传Data,File图片,文件等上传(转) afnetworking断点上传

  

爱华网本文地址 » http://www.413yy.cn/a/25101012/131689.html

更多阅读

2014.6.21河汉《断点续传》☆+ 断点续传by河汉

河汉《断点续传》(远空 哨兵向导设定 年下强强 热血军文 幽默有狗血有天雷急躁闷骚强势攻&慢热温和聪明受)推荐指数:★☆☆6.17看到河汉大的新文隆重登场时,小惊喜了一把,要知道当年这位大人的《最终流放》可是风靡了一整年的热血

如何上传图片到淘宝的图片空间? 淘宝图片空间高速上传

如何上传图片至淘宝相册1、打开我的淘宝-- 图片空间 2、选择你需要上传图片的文件夹 3、进入某文件夹后,点击 上传图片4、选择您需要上传的图片,每次上传最多只能上传10张 5、至此点击 完成即可备注:如果您要使用相册中的图片的话,只需

如何下载和重新上传新浪博客视频? 新浪博客无法上传图片

如果你是新浪博客的写手,你是否发现以前上传到博客正文里的视频都已被删除?而且,在很长一段时间里,新浪博客也不提供原有视频的下载,一度令许多博文支离破碎,许多博友对此都很有意见。其实,我早就发现这个问题,那时,我正在编辑我的博客电子

声明:《AFNetworking文件上传Data,File图片,文件等上传(转) afnetworking断点上传》为网友马上有对象分享!如侵犯到您的合法权益请联系我们删除