的文本 XXXXXXXXXX在自己的论点更填写
- (void)uploadImageWithImage:(NSString *)imagePath
{
//上传其它所需參数
NSString *userId=XXXXXXXXXXX;
NSString *token=XXXXXXXXXXX;
//上传请求POST
AFHTTPClient *client=[AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@""]];
NSString *urlString=[NSString stringWithFormat:@"上传server地址];
NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:userId,@"XXXXXX",token,@"XXXXXX", nil];
NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:urlString parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//得到须要上传的数据
NSData *data=[NSData dataWithContentsOfFile:imagePath];
//上传时使用当前的系统事件作为文件名称
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
/*
此方法參数
1. 要上传的[二进制数据]
2. 相应站点上[upload.php中]处理文件的[字段"file"]
3. 要保存在server上的[文件名称]
4. 上传文件的[mimeType]
*/
//server上传文件的字段和类型
[formData appendPartWithFileData:data name:@"XXXXX" fileName:fileName mimeType:@"image/jpg/file"];
}];
// 3. operation包装的urlconnetion
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传完毕");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败->%@", error);
}];
//运行
[client.operationQueue addOperation:op];
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。