日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

afn post请求上传文件_iOS利用AFNetworking(AFN) 实现图片上传

發布時間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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) 实现图片上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。