当前位置:网站首页>Afnetworking framework_ Upload file or image server

Afnetworking framework_ Upload file or image server

2022-07-06 18:26:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

The text of XXXXXXXXXX Fill in your argument more

– (void)uploadImageWithImage:(NSString *)imagePath

{

// Upload other required parameters

NSString *userId=XXXXXXXXXXX;

NSString *token=XXXXXXXXXXX;

// Upload request POST

AFHTTPClient *client=[AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@””]];

NSString *urlString=[NSString stringWithFormat:@” Upload server Address ];

NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:userId,@”XXXXXX”,token,@”XXXXXX”, nil];

NSURLRequest *request = [client multipartFormRequestWithMethod:@”POST” path:urlString parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

// Get the data you need to upload

NSData *data=[NSData dataWithContentsOfFile:imagePath];

// When uploading, use the current system event as the file name

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

formatter.dateFormat = @”yyyyMMddHHmmss”;

NSString *str = [formatter stringFromDate:[NSDate date]];

NSString *fileName = [NSString stringWithFormat:@”%@.jpg”, str];

/*

This method parameter

1. To upload [ binary data ]

2. On the corresponding site [upload.php in ] Handling documents [ Field ”file”]

3. To keep in server Upper [ File name ]

4. Of uploaded files [mimeType]

*/

//server Fields and types of uploaded files

[formData appendPartWithFileData:data name:@”XXXXX” fileName:fileName mimeType:@”image/jpg/file”];

}];

// 3. operation Packaged urlconnetion

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@” Upload finished ”);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@” Upload failed ->%@”, error);

}];

// function

[client.operationQueue addOperation:op];

}

Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117393.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061018309544.html