afn post请求上传文件_iOS利用AFNetworking(AFN) 实现图片上传
1.上傳圖片以二進制流的形式上傳
1 #pragma
mark - 文件上傳
2 - (IBAction)uploadImage
3 {
4
10 ? ? // 1.
httpClient->url
11
12 ? ? // 2.
上傳請求POST
13 ? ? NSURLRequest
*request = [_httpClient multipartFormRequestWithMethod:@"POST"
path:@"upload.php" parameters:nil constructingBodyWithBlock:^(id
formData) {
14
//
在此位置生成一個要上傳的數據體
15
//
form對應的是html文件中的表單
16
17
18
UIImage *image = [UIImage
imageNamed:@"頭像1"];
19
NSData *data =
UIImagePNGRepresentation(image);
20
21
//
在網絡開發中,上傳文件時,是文件不允許被覆蓋,文件重名
22
// 要解決此問題,
23
//
可以在上傳時使用當前的系統事件作為文件名
24
NSDateFormatter *formatter =
[[NSDateFormatter alloc] init];
25
// 設置時間格式
26
formatter.dateFormat =
@"yyyyMMddHHmmss";
27
NSString *str = [formatter
stringFromDate:[NSDate date]];
28
NSString *fileName =
[NSString stringWithFormat:@"%@.png", str];
29
30
31
38
[formData
appendPartWithFileData:data name:@"file" fileName:fileName
mimeType:@"image/png"];
39
}];//file改為后臺接收的字段或參數
40
41 ? ? // 3.
operation包裝的urlconnetion
42
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
43
44 ? ? [op
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
id responseObject) {
45
NSLog(@"上傳完成");
46 ? ? }
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
47
NSLog(@"上傳失敗->%@", error);
48
}];
49
50
//執行
51
[_httpClient.operationQueue addOperation:op];
當要上傳多張圖片時只需在multipartFormRequestWithMethod方法上添加這些代碼就好
AFNetWorking使用multipartFormRequestWithMethod方法上傳多張圖片問題
int i=0;
NSMutableURLRequest *request = [[AFNetWorkSingleton
shareInstance] multipartFormRequestWithMethod:@"POST"
path:@"Mindex/getimg" parameters:nil
constructingBodyWithBlock:^(idformData){
for(UIImage
*eachImage in array)
{
NSData *imageData =
UIImageJPEGRepresentation(eachImage,0.5);
[formData appendPartWithFileData:imageData
name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString
stringWithFormat:@"abc%d.jpg",i ]
mimeType:@"image/jpeg"];//file改為后臺接收的字段或參數
i++;
}
}];
2.上傳圖片以二進制流的字符串的形式上傳
-(void)postPhotosToShare_API23_withPid:(NSString *)_pid
andUid:(NSString *)_uid andScore:(float)_score andContent:(NSString
*)_content andAnonymous:(NSString *)_anonymous
andImgArray:(NSMutableArray *)_imgArray
{
path =
@"interface/product.php/product/";//path為網站開發人員告知的除去IP后的地址
NSURL *baseUrl1 = [NSURL
URLWithString:urlIP];//urlIP為網站開發人員告知的IP地址,例:http://192..168.1.1
httpClient =
[[AFHTTPClient alloc]initWithBaseURL:baseUrl1];
NSMutableDictionary
*parameters = [[NSMutableDictionary alloc]init];
[parameters setObject:_pid
forKey:@"pid"];
[parameters setObject:_uid
forKey:@"uid"];
[parameters
setObject:[NSString stringWithFormat:@"%f",_score]
forKey:@"score"];
[parameters
setObject:_content forKey:@"content"];
[parameters
setObject:_anonymous forKey:@"anonymous"];
if
(_imgArray.count!=0)
{
int imgCount=0;
for (UIImage *myImg in _imgArray)
{
NSData
*imageData = UIImageJPEGRepresentation(myImg,0.7);//進行圖片壓縮
NSString
*_encodedImageStr = [imageData base64Encoding];//進行64位轉碼轉為字符串
[parameters setObject:_encodedImageStr forKey:[NSString
stringWithFormat:@"img[%i]",imgCount]];//進行img[%i]改為后臺接收的字段或參數
imgCount
++;
}
}
request = [httpClient
requestWithMethod:@"POST" path:path parameters:parameters];
[request
setTimeoutInterval:kDataExpiryTime];//設置請求時間
[AFJSONRequestOperation
addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation
*operation = [[AFJSONRequestOperation
alloc]initWithRequest:request];
[operation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:operation.responseData
options:NSJSONReadingMutableContainers error:nil];
[self getResultSuccess:json
withTage:Get_API_Tag_23];//對api進行標記,可要可不要
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self getResultFailed:error];
}];
[operation start];
}
總結
以上是生活随笔為你收集整理的afn post请求上传文件_iOS利用AFNetworking(AFN) 实现图片上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java数据库连接协议JDBC学习
- 下一篇: 链表(单链表、双链表、内核链表)