图片上传
来源:互联网 发布:移动网络里没有运营商 编辑:程序博客网 时间:2024/06/14 23:05
下面是图片上传的方法:
-(void)loadImage:(NSString*)aurl
{
NSData *imageData;
NSMutableData *postBody;
NSString *stringBoundary, *contentType;
NSURL *url = [NSURL URLWithString:aurl]; //将字符串转换为NSURL格式
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"1.jpg"];
imageData = [[NSData alloc] initWithContentsOfFile:path];
ASIFormDataRequest *aRequest = [[ASIFormDataRequest alloc] initWithURL:url];
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[aRequest addRequestHeader:@"Content-Type" value:contentType];
postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post
NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttablexxxxxxxx"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[aRequest setDelegate:self];
[aRequest appendPostData:postBody];
[aRequest setRequestMethod:@"POST"];
[aRequest startAsynchronous];
[postBody release];
[aRequest release];
}
-(void)loadImage:(NSString*)aurl
{
NSData *imageData;
NSMutableData *postBody;
NSString *stringBoundary, *contentType;
NSURL *url = [NSURL URLWithString:aurl]; //将字符串转换为NSURL格式
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"1.jpg"];
imageData = [[NSData alloc] initWithContentsOfFile:path];
ASIFormDataRequest *aRequest = [[ASIFormDataRequest alloc] initWithURL:url];
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[aRequest addRequestHeader:@"Content-Type" value:contentType];
postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post
NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttablexxxxxxxx"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[aRequest setDelegate:self];
[aRequest appendPostData:postBody];
[aRequest setRequestMethod:@"POST"];
[aRequest startAsynchronous];
[postBody release];
[aRequest release];
}
0 0
- 图片上传
- 上传图片
- 上传图片
- 上传图片
- 上传图片
- 上传图片
- 图片上传
- 图片上传
- 上传图片
- 上传图片
- 图片上传
- 图片上传
- 图片上传
- 图片上传
- 上传图片
- 上传图片
- 上传图片
- 图片上传
- 当下流行的分布式文件系统大阅兵
- 新编辑器Cocos Creator发布:对不起我来晚了!
- java分页工具类
- 自定义异常/导包/JUnit/设置快捷键/ myeclipse自带的数据库/生成API的html文档
- jquery 处理返回数据 html json xml
- 图片上传
- Linux curl命令参数详解--转载
- History 对象
- 初始化方法
- SQLyog导入数据库
- 40个Java多线程问题小结
- Android类似qq消息滑动菜单
- [破解实例][OllyDbg] CrackMe001-Acid burn
- Visual studio中编译和使用libpng和zlib