当前位置:网站首页>AFNetworking框架_上传文件或图像server
AFNetworking框架_上传文件或图像server
2022-07-06 10:19:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
的文本 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];
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117393.html原文链接:https://javaforall.cn
边栏推荐
- 编译原理——预测表C语言实现
- 递归的方式
- Jerry's access to additional information on the dial [article]
- std::true_ Type and std:: false_ type
- 重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
- Rb157-asemi rectifier bridge RB157
- FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
- 2022 Summer Project Training (II)
- [.Net core] solution to error reporting due to too long request length
- Principle and usage of extern
猜你喜欢

关于这次通信故障,我想多说几句…

Getting started with pytest ----- test case rules

2019 Alibaba cluster dataset Usage Summary

1700C - Helping the Nature

递归的方式

Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti

Recommend easy-to-use backstage management scaffolding, everyone open source
![Jerry is the custom background specified by the currently used dial enable [chapter]](/img/32/6c22033bda8ff1b53993bacef254cd.jpg)
Jerry is the custom background specified by the currently used dial enable [chapter]

std::true_type和std::false_type

阿里云国际版ECS云服务器无法登录宝塔面板控制台
随机推荐
Rb157-asemi rectifier bridge RB157
2022 Summer Project Training (III)
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
MSF horizontal MSF port forwarding + routing table +socks5+proxychains
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
Getting started with pytest ----- test case pre post, firmware
OpenEuler 会长久吗
[Android] kotlin code writing standardization document
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
Markdown grammar - better blogging
2022 Summer Project Training (II)
Introduction to the usage of model view delegate principal-agent mechanism in QT
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
递归的方式
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
J'aimerais dire quelques mots de plus sur ce problème de communication...
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
2022暑期项目实训(一)