ASIFormDataRequest 上传图片
2024-09-24 00:17:19
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. [self request];
} - (void)saveImage
{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[imageData writeToFile:[self GetTempPath] atomically:YES];
} -(NSString *)GetTempPath{
NSString *tempPath =[NSString stringWithFormat:@"%@/upload.jpg",NSTemporaryDirectory()];
return tempPath;
} - (void)request
{
NSDictionary *postDic = [[NSDictionary alloc]init];
[postDic setValue:@"test" forKey:@"userId"];
[postDic setValue:[self GetTempPath] forKey:@"file"]; [self uploadImage:postDic];
} - (void)uploadImage:(NSDictionary *)dic
{
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:postUrl]]; // 通常数据是以’application/x-www-form-urlencoded’格式发送的,如果上传了二进制数据或者文件,那么格式将自动变为‘multipart/form-data’注意要和服务器统一。
[request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];
//超时时间
request.timeOutSeconds = ; //定义异步方法
[request setDelegate:self];
[request setRequestMethod:@"POST"];
[request setDidFailSelector:@selector(commonRequestDidFailed:)];
[request setDidFinishSelector:@selector(commonRequestDidSuccess:)]; //post的数据
id key, value;
for (int i = ; i < [dic allKeys].count; i++)
{ key = [keys objectAtIndex: i];
value = [params objectForKey: key];
NSLog (@"Key: %@ for value: %@", key, value); if ([key isEqualToString:@"file"]) {
[request setFile:value forKey:key];
[request setShouldStreamPostDataFromDisk:YES];
}
else
{
[request setPostValue:value forKey:key];
} } [request startSynchronous];
}
最新文章
- 【原创】SQL审核系统
- 比较核心的技术了 虚拟ip的一种实现方式(手工添加和C#添加)
- [Hibernate] - Annotations
- python(二)拾遗
- 【学习笔记】【C语言】数据
- MIT 2012分布式课程基础源码解析-线程池实现
- [置顶] 【Git入门之十一】标签管理
- C#线程池ThreadPool的理解
- 使用Z3破解简单的XOR加密
- 蛋疼的_after_insert
- [Codeforces702F]T-Shirts——非旋转treap+贪心
- 【XSY3154】入门多项式 高斯消元
- 《剑指offer》 二维数组中的查找
- gitlab--ci文件
- AIX中vi编辑器使用
- openresty 一些可选的模板引擎
- 调用azkaban接口,upload 本地zip文件
- Strategic Game HDU - 1054(最小顶点覆盖)
- Cognos11中通过URL传参访问动态Report
- Idea的注入和自动编译配置